Dependency Injection

Component: NServiceBus
NuGet Package NServiceBus (7.x - 7.1)

NServiceBus automatically registers and invokes message handlers, sagas, and other user-provided extension points using a dependency injection container.

Container configuration

Built-in default container

NServiceBus has a built-in default container with an API for registration of user types. The following dependency lifecycles are supported:

Instance per call

A new instance will be returned for each call.

Represented by the enum value DependencyLifecycle.InstancePerCall.

endpointConfiguration.RegisterComponents(
    registration: configureComponents =>
    {
        configureComponents.ConfigureComponent<MyService>(DependencyLifecycle.InstancePerCall);
    });

or using a delegate:

endpointConfiguration.RegisterComponents(
    registration: configureComponents =>
    {
        configureComponents.ConfigureComponent(
            componentFactory: () =>
            {
                return new MyService();
            },
            dependencyLifecycle: DependencyLifecycle.InstancePerCall);
    });

Instance per unit of work

The instance will be a 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.

endpointConfiguration.RegisterComponents(
    registration: configureComponents =>
    {
        configureComponents.ConfigureComponent<MyService>(DependencyLifecycle.InstancePerUnitOfWork);
    });

or using a delegate:

endpointConfiguration.RegisterComponents(
    registration: configureComponents =>
    {
        configureComponents.ConfigureComponent(
            componentFactory: () =>
            {
                return new MyService();
            },
            dependencyLifecycle: DependencyLifecycle.InstancePerUnitOfWork);
    });

Single instance

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 as SingleInstance, will behave as if they are SingleInstance because the instances will exist inside the parent component.
endpointConfiguration.RegisterComponents(
    registration: configureComponents =>
    {
        configureComponents.ConfigureComponent<MyService>(DependencyLifecycle.SingleInstance);
    });

or using a delegate:

endpointConfiguration.RegisterComponents(
    registration: configureComponents =>
    {
        configureComponents.ConfigureComponent(
            componentFactory: () =>
            {
                return new MyService();
            },
            dependencyLifecycle: DependencyLifecycle.SingleInstance);
    });

or using the explicit singleton API:

endpointConfiguration.RegisterComponents(
    registration: configureComponents =>
    {
        configureComponents.RegisterSingleton(new MyService());
    });

Using a third party container

NServiceBus also supports the following third party containers:

Plugging in other containers

If a specific library is not supported, 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
{

Create a class that implements 'ContainerDefinition' and returns the 'IContainer' implementation:

public class MyContainerDefinition :
    ContainerDefinition
{
    public override IContainer CreateContainer(ReadOnlySettings settings)
    {
        return new MyContainer();
    }
}

Then register the ContainerDefinition to be used:

endpointConfiguration.UseContainer<MyContainerDefinition>();

Injecting the endpoint instance

IEndpointInstance is not registered automatically and must be registered explicitly to be injected.

In NServiceBus version 6 and above IEndpointInstance is not IDisposable.

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 containers
    Child containers allow for more granular instance lifetime configuration.
  • 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