NServiceBus supports two modes of operation for containers, internally managed and externally managed.
In internally managed mode, NServiceBus manages the entire lifecycle of the container, including registration, component resolution, and disposal.
NServiceBus uses the Microsoft.Extensions.DependencyInjection
container by default. Custom services may be registered using the IServiceCollection
API.
A new instance will be returned for each call.
Represented by the enum value ServiceLifetime.Transient
.
snippet: InstancePerCall
or using a delegate:
snippet: DelegateInstancePerCall
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 ServiceLifetime.Scoped
.
snippet: InstancePerUnitOfWork
or using a delegate:
snippet: DelegateInstancePerUnitOfWork
The same instance will be returned each time.
Represented by the enum value ServiceLifetime.Singleton
.
WARNING: Singleton
components with dependencies that are scoped Transient
or Scoped
will still resolve. In effect, these dependencies, while not scoped as Singleton
, will behave as if they are Singleton
because the instances will exist inside the parent component.
snippet: SingleInstance
or using a delegate:
snippet: DelegateSingleInstance
Third party or custom dependency injection containers can be used via the externally managed mode.
In externally managed mode, NServiceBus registers its components in the container but does not own the container's lifecycle. NServiceBus uses the Microsoft.Extensions.DependencyInjection
API to integrate with third party containers.
WARN: 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 IServiceCollection
is passed to the EndpointWithExternallyManagedContainer.Create
method. The following snippets show how to use Microsoft's default implementation from the Microsoft.Extensions.DependencyInjection
NuGet package:
snippet: ExternalPrepare
Later, during the resolution phase, the Start
method requires an instance of IServiceProvider
.
snippet: ExternalStart
NOTE: Refer to the container's documentation on how to use the container with the Microsoft.Extensions.DependencyInjection
API.
IMessageSession
is not registered automatically in the container and must be registered explicitly to be injected. Access to the session is provided via IStartableEndpointWithExternallyManagedContainer.MessageSession
Note: The session is only valid for use after the endpoint have been started, so it is provided as Lazy<IMessageSession>
.
The NServiceBus.Extensions.DependencyInjection Usage sample demonstrates how to register the message session.
When hosting NServiceBus with the Microsoft Generic Host using the NServiceBus.Extensions.Hosting
package, refer to the configure custom containers documentation for further details.
It is recommended to follow the dependency injection guidelines for .NET. Be aware of the following special cases with NServiceBus: