Ninject Container Usage

Component: Ninject Container | Nuget: NServiceBus.Ninject (Version: 6.x)
Target NServiceBus Version: 6.x

Configuring NServiceBus to use the container

Edit
var endpointConfiguration = new EndpointConfiguration("Samples.Ninject");

var kernel = new StandardKernel();
kernel.Bind<MyService>()
    .ToConstant(new MyService());
endpointConfiguration.UseContainer<NinjectBuilder>(
    customizations: customizations =>
    {
        customizations.ExistingKernel(kernel);
    });

Injecting the dependency in the handler

Edit
public class MyHandler :
    IHandleMessages<MyMessage>
{
    MyService myService;

    public MyHandler(MyService myService)
    {
        this.myService = myService;
    }

    public Task Handle(MyMessage message, IMessageHandlerContext context)
    {
        myService.WriteHello();
        return Task.CompletedTask;
    }
}

Related Articles

  • Containers
    NServiceBus automatically registers components, handlers, and sagas.
  • Ninject
    Configure NServiceBus to use Ninject as a container.

Last modified