Ninject Dependency Injection Usage

Component: Ninject Dependency Injection
NuGet Package NServiceBus.Ninject (5.x)
Target NServiceBus Version: 5.x

Configuring to use Ninject

var busConfiguration = new BusConfiguration();
busConfiguration.EndpointName("Samples.Ninject");

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

Injecting the dependency in the handler

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

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

    public void Handle(MyMessage message)
    {
        myService.WriteHello();
    }
}

Related Articles

  • Dependency Injection
    NServiceBus automatically registers components, handlers, and sagas.
  • Ninject
    Configure NServiceBus to use Ninject for dependency injection.

Last modified