Containers

Component: NServiceBus | Nuget: NServiceBus (Version: 4.x)
Standard support for version 4.x of NServiceBus has expired. For more information see our Support Policy.

Services and state are managed by Containers and Dependency Injection. 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 container (currently an ILMerged version of Autofac) 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

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

Delegate Registration

Edit
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

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

Delegate Registration

Edit
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

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

Delegate Registration

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

Register Single Instance

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

Supported Containers

Support for other containers is provided via custom integrations.

Using an existing container

The above pages all have examples of how to pass in an instance of an existing container. This is useful to make use of the full features of the container and share the DI 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 container 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 an external container is not used, the bus instance still needs to be properly disposed of by calling IBus.Dispose().

Plugging in the container

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

Create a class that implements 'IContainer':

Edit
public class MyContainer :
    IContainer
{

Then register the IContainer to be used:

Edit
configure.UsingContainer<MyContainer>();

Samples

Related Articles

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

Last modified