Dependency Injection

Component: NServiceBus
NuGet Package NServiceBus (6.x)

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

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

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 all NServiceBus services will be injected into the passed in dependency injection 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

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
{

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