Code Monkey home page Code Monkey logo

Comments (5)

TDK1964 avatar TDK1964 commented on September 28, 2024

I've fixed the error by changing the registration to this

builder.RegisterSource(new ContravariantRegistrationSource());
builder.RegisterAssemblyTypes(typeof(IMediator).Assembly).AsSelf().AsImplementedInterfaces();
builder.Register<SingleInstanceFactory>(ctx =>
{
    var c = ctx.Resolve<IComponentContext>();
    return t => c.Resolve(t);
});
builder.Register<MultiInstanceFactory>(ctx =>
{
    var c = ctx.Resolve<IComponentContext>();
    return t => (IEnumerable<object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(t));
});

But now it doesn't work with async actions

The requested service 'MediatR.IAsyncRequestHandler`2[[OfficeManager.Features.People.Edit+Query, OfficeManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[OfficeManager.Features.People.Edit+Command, OfficeManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

from mediatr.

joaomatossilva avatar joaomatossilva commented on September 28, 2024

Hi @TDK1964

you need to also register on Autofac your handlers. Try adding the following line after:

        builder.RegisterAssemblyTypes(assembly)
            .AsClosedTypesOf(typeof(IRequestHandler<,>))
            .AsImplementedInterfaces();

        builder.RegisterAssemblyTypes(assembly)
            .AsClosedTypesOf(typeof(IAsyncRequestHandler<,>))
            .AsImplementedInterfaces();

        builder.RegisterAssemblyTypes(assembly)
            .AsClosedTypesOf(typeof(INotificationHandler<>))
            .AsImplementedInterfaces();

        builder.RegisterAssemblyTypes(assembly)
            .AsClosedTypesOf(typeof(IAsyncNotificationHandler<>))
            .AsImplementedInterfaces();

from mediatr.

TDK1964 avatar TDK1964 commented on September 28, 2024

@kappy thanks for the feedback.

I actually did this while I was waiting for replies and it seemed to work. Is this acceptable?

 builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
               .Where(t => t.Name.EndsWith("Handler"))
               .AsImplementedInterfaces()
               .InstancePerRequest();

from mediatr.

joaomatossilva avatar joaomatossilva commented on September 28, 2024

That is more a Autofac related case other than MediatR.
I don't know all the considerations about using the name string compare vs the type.

from mediatr.

TDK1964 avatar TDK1964 commented on September 28, 2024

@kappy, on a seprate note, do you know how to convert this the autofac?


private static IElementGenerator<T> GetGenerator<T>(T model) where T : class
{
      var library = StructuremapMvc.ParentScope.CurrentNestedContainer.GetInstance<HtmlConventionLibrary>();
      return ElementGenerator<T>.For(library, t => StructuremapMvc.ParentScope.CurrentNestedContainer.GetInstance(t), model);
}

from mediatr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.