Giter Site home page Giter Site logo

Comments (4)

anghiulian avatar anghiulian commented on July 20, 2024 1

Thank you! The solution you provided works!

from mediatr.

kiwipiet avatar kiwipiet commented on July 20, 2024

It looks like your bindings is not setup correctly.
Could you post your code where you setup your Ninject kernel?

from mediatr.

anghiulian avatar anghiulian commented on July 20, 2024

Hi @kiwipiet,

The code inside NinjectWebCommon is as follows:

 private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        } 
private static void RegisterServices(IKernel kernel)
        {
            kernel.Components.Add<IBindingResolver, ContravariantBindingResolver>();
            kernel.Bind(scan => scan.FromAssemblyContaining<IMediator>).SelectAllClasses().BindDefaultInterface());           
            kernel.Bind(scan => scan.FromAssemblyContaining(typeof(IAsyncRequestHandler<,>)).SelectAllClasses().BindAllInterfaces());
            kernel.Bind(scan => scan.FromAssemblyContaining(typeof(IAsyncRequest<>)).SelectAllClasses().BindAllInterfaces());
            kernel.Bind<SingleInstanceFactory>().ToMethod(ctx => t => ctx.Kernel.Get(t));
            kernel.Bind<MultiInstanceFactory>().ToMethod(ctx => t => ctx.Kernel.GetAll(t));
}

And I try to inject it in a ApiController like so:

        private readonly IMediator mediator;

        public PeopleController(IMediator mediator)
        {
            this.mediator = mediator;
        }

from mediatr.

kiwipiet avatar kiwipiet commented on July 20, 2024

kernel.Bind(scan => scan.FromAssemblyContaining(typeof(IAsyncRequestHandler<,>)).SelectAllClasses().BindAllInterfaces()); kernel.Bind(scan => scan.FromAssemblyContaining(typeof(IAsyncRequest<>)).SelectAllClasses().BindAllInterfaces());

These are your problem lines. The assembly it is going to find in both cases is 'MediatR.dll'. And this assembly has already been bound in:
kernel.Bind(scan => scan.FromAssemblyContaining<IMediator>).SelectAllClasses().BindDefaultInterface());

If you have one assembly that contains your handlers and requests then only register that assembly once and don't re-register MediatR.dll

This is clearly a web app, and if you have all your requests and handlers in the web app assembly you could do the following:

kernel.Bind(scan => scan.From(Assembly.GetExecutingAssembly()).SelectAllClasses().InNamespaceOf(type).BindAllInterfaces());

or

kernel.Bind(scan => scan.FromAssemblyContaining<MyAsyncRequestHandler>).SelectAllClasses().BindDefaultInterface());
where MyAsyncRequestHandler is one of your handlers.

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.