Project Hosting
NuGet Package NServiceBus.Ninject (4.x)
Target NServiceBus Version: 4.x
Standard support for version 4.x of NServiceBus has expired. For more information see our Support Policy.

NServiceBus can be configured to use Ninject for dependency injection.

Default Usage


Existing Instance

var kernel = new StandardKernel();
    .ToConstant(new MyService());

Unit of work

Its is possible to bind to use an Unit of Work scope, which corresponds to the DependencyLifecycle.InstancePerUnitOfWork lifecycle, when registering components with configuration.RegisterComponents(...).

In essence, bindings using Unit of Work scope

  • will be instantiated only once per transport Message
  • will be disposed when message processing finishes

Bind the services in Unit of Work scope using:

var kernel = new StandardKernel();


Services using InUnitOfWorkScope() can only be injected into code which is processing messages. To inject the service somewhere else (e.g. because of an user interaction) define conditional bindings:

var kernel = new StandardKernel();

// always create a new instance when not processing a message

// always use the same instance when processing messages

DependencyLifecycle Mapping

The DependencyLifecycle maps to Ninject in the following way.

DependencyLifecycleNinject Equivalent
InstancePerUnitOfWorkSingleton within a Named Scope per Unit of Work


Related Articles

  • Child lifetime
    Child lifetimes are a snapshot of the main instance; transient instances are treated as as singletons in the child lifetime.

Last modified