NServiceBus automatically registers and invokes message handlers, sagas, and other user-provided extension points using a dependency injection container.
NServiceBus supports two modes of operation for containers, internally managed and externally managed.
Internally managed mode
In internally managed mode, NServiceBus manages the entire lifecycle of the container, including registration, component resolution, and disposal.
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.
.
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.
.
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
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>();
Externally managed mode
In externally managed mode, NServiceBus registers its components in the container but does not own the container's lifecycle. The container is provided by the user in two phases, one for registration (IConfigureComponents
) and one for resolution (IBuilder
).
Every NServiceBus endpoint requires its own dependency injection container. Sharing containers across multiple endpoints results in conflicting registrations and might cause incorrect behavior or runtime errors.
During the registration phase, an instance of IConfigureComponents
is passed to the EndpointWithExternallyManagedContainer.
method. For example, for Autofac's ContainerBuilder
, this is the phase during which its type registration methods would be called.
IConfigureComponents configureComponents =
AdaptContainerForRegistrationPhase(myCustomContainer);
var startableEndpoint = EndpointWithExternallyManagedContainer.Create(endpointConfiguration, configureComponents);
Later, during the resolution phase, the Start
method requires an instance of IBuilder
. At this stage, the container has already been initialized with all its registrations. For example, for Autofac's ContainerBuilder
, this is the phase during which its Build
method would be called.
IBuilder builder = AdaptContainerForResolutionPhase(myCustomContainer);
var startedEndpoint = await startableEndpoint.Start(builder);
The Adapt
methods are implemented by the user and are container-specific. NServiceBus.Extensions.DependencyInjection supports externally managed mode using Microsoft.
abstractions (IServiceCollection
and IServiceProvider
) that are supported by most dependency injection containers.
Injecting the message session
IMessageSession
is not registered automatically in the container and must be registered explicitly to be injected. Access to the session is provided via IStartableEndpointWithExternallyManagedContainer.
The session is only valid for use after the endpoint have been started, so it is provided as Lazy
.
The NServiceBus.Extensions.DependencyInjection Usage sample demonstrates how to register the message session.