Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Ninject Dependency Injection Usage

NuGet Package: NServiceBus.Ninject (7.x)
Target Version: NServiceBus 7.x

Configuring an endpoint to use Ninject

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

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

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