Dependency Injection

Component: NServiceBus
NuGet Package NServiceBus (3.x)
Standard support for version 3.x of NServiceBus has expired. For more information see our Support Policy.

Services and state are managed by Dependency Injection (DI). NServiceBus automatically registers all its components as well as user-implemented handlers and sagas so that all instancing modes and wiring are done correctly by default and without errors. NServiceBus has a built-in DI, but it can be replaced by any other container.

Dependency Lifecycle

There are three modes of Dependency Lifecycle.

InstancePerCall

A new instance will be returned for each call.

Represented by the enum value DependencyLifecycle.InstancePerCall.

Registration

var components = configure.Configurer;
components.ConfigureComponent<MyService>(DependencyLifecycle.InstancePerCall);

Delegate Registration

var components = configure.Configurer;
components.ConfigureComponent(
    componentFactory: () =>
    {
        return new MyService();
    },
    dependencyLifecycle: DependencyLifecycle.InstancePerCall);

InstancePerUnitOfWork

The instance will be singleton for the duration of the unit of work. In practice this means the processing of a single transport message.

Represented by the enum value DependencyLifecycle.InstancePerUnitOfWork.

Registration

var components = configure.Configurer;
components.ConfigureComponent<MyService>(DependencyLifecycle.InstancePerUnitOfWork);

Delegate Registration

var components = configure.Configurer;
components.ConfigureComponent(
    componentFactory: () =>
    {
        return new MyService();
    },
    dependencyLifecycle: DependencyLifecycle.InstancePerUnitOfWork);

SingleInstance

The same instance will be returned each time.

Represented by the enum value DependencyLifecycle.SingleInstance.

SingleInstance components that have dependencies that are scoped InstancePerCall or InstancePerUnitOfWork will still resolve. In effect these dependencies, while not scoped SingleInstance, will behave as if they are SingleInstance as the instances will exist inside the parent component.

Registration

var components = configure.Configurer;
components.ConfigureComponent<MyService>(DependencyLifecycle.SingleInstance);

Delegate Registration

var components = configure.Configurer;
components.ConfigureComponent(
    componentFactory: () =>
    {
        return new MyService();
    },
    dependencyLifecycle: DependencyLifecycle.SingleInstance);

Register Single Instance

var components = configure.Configurer;
components.RegisterSingleton<MyService>(new MyService());

Supported

Support for other containers is provided via custom integrations.

Using an existing instance

The above pages all have examples of how to pass in an instance. This is useful to make use of the full features and share the behavior with NServiceBus.

Endpoint resolution

Note that the instance of IBus is scoped for the lifetime of the container. Hence to resolve IBus and then dispose of it the endpoint will stop processing messages. Note that all NServiceBus services, including IBus, will be injected into the passed in dependency injection instance. As such there is no need to register these instances at configuration time.

Cleanup

When using an external container, the bus instance is not automatically disposed. To dispose of the resources properly:

  1. Call IBus.Dispose() to dispose of the bus.
  2. And then call container.Dispose() to dispose of the container.
When external dependency injection is not used, the bus instance still needs to be properly disposed of by calling IBus.Dispose().

Plugging in

If a specific library is not already supported, then create a plugin using the IContainer abstraction. Once this is created and registered, NServiceBus will use the custom dependency injection to look up its own dependencies.

Create a class that implements 'IContainer':

public class MyContainer :
    IContainer
{

Then register the IContainer to be used:

// Create a class that implements 'IContainer'
configure.UsingContainer<MyContainer>();

Samples

Related Articles

  • Autofac
    Details on how to Configure NServiceBus to use Autofac for dependency injection. Includes usage examples as well as lifecycle mappings.
  • Castle Windsor
    Details on how to Configure NServiceBus to use Castle Windsor for dependency injection. Includes usage examples as well as lifecycle mappings.
  • Child lifetime
    Child lifetimes are a snapshot of the main instance; transient instances are treated as as singletons in the child lifetime.
  • Ninject
    Configure NServiceBus to use Ninject for dependency injection.
  • Property injection
  • Simple Injector
    Configure NServiceBus to use Simple Injector for dependency injection.
  • Spring
    Details on how to Configure NServiceBus to use Spring for dependency injection. Includes usage examples as well as lifecycle mappings.
  • StructureMap
    Details on how to Configure NServiceBus to use StructureMap for dependency injection. Includes usage examples as well as lifecycle mappings.
  • Unity
    Details on how to Configure NServiceBus to use Unity for dependency injection. Includes usage examples as well as lifecycle mappings.

Last modified