Containers

Component: NServiceBus
NuGet NServiceBus (7-pre)
This page targets a pre-release version and is subject to change prior to the final release.

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

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

Delegate Registration

endpointConfiguration.RegisterComponents(
    registration: configureComponents =>
    {
        configureComponents.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

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

Delegate Registration

endpointConfiguration.RegisterComponents(
    registration: configureComponents =>
    {
        configureComponents.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

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

Delegate Registration

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

Register Single Instance

endpointConfiguration.RegisterComponents(
    registration: configureComponents =>
    {
        configureComponents.RegisterSingleton(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 all NServiceBus services will be injected into the passed in container instance. As such there is no need to register these instances at configuration time.

IEndpointInstance needs to be registered to be properly resolved.

Cleanup

In Version 6 IEndpointInstance is not IDisposable.

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':

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>();

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