Skip to content

Latest commit

 

History

History
92 lines (52 loc) · 4.55 KB

extensions-dependencyinjection.md

File metadata and controls

92 lines (52 loc) · 4.55 KB
title summary reviewed component related redirects
NServiceBus.Extensions.DependencyInjection
Integration with Microsoft.Extensions.DependencyInjection
2023-06-02
Extensions.DependencyInjection
samples/dependency-injection/extensions-dependency-injection
samples/hosting/generic-host
nservicebus/dependency-injection/property-injection
nservicebus/property-injection-in-handlers

Warning

Starting with NServiceBus version 8, the NServiceBus.Extensions.DependencyInjection package is no longer required. NServiceBus directly supports the Microsoft.Extensions.DependencyInjection model via the externally managed container mode. Visit the dependency injection upgrade guide for further information.

The NServiceBus.Extensions.DependencyInjection package provides integration with the Microsoft.Extensions.DependencyInjection dependency injection abstraction.

Note

It's recommended to use Microsoft Generic Host to manage application and dependency injection container lifecycle. Use the NServiceBus.Extensions.Hosting package to host an NServiceBus endpoint with the generic host.

Usage with ServiceCollection

The following snippet shows how to configure NServiceBus to use Microsoft's built-in dependency injection container:

snippet: usecontainer-servicecollection

Usage with third party containers

NServiceBus can be configured to work with any third party dependency injection container which implements the Microsoft.Extensions.DependencyInjection abstraction. To use a third-party dependency injection container, pass the specific container's IServiceProviderFactory to the UseContainer configuration method.

Autofac

snippet: AutofacUsage

Castle Windsor

snippet: CastleUsage

StructureMap

snippet: StructureMapUsage

Unity

snippet: UnityUsage

Configuring the container

UseContainer provides a settings class which gives advanced configuration options:

IServiceCollection access

The settings provide access to the underlying IServiceCollection that can be used to add additional service registrations.

snippet: settings-servicecollection

ContainerBuilder

Third-party container-native APIs can be accessed by with ConfigureContainer.

snippet: settings-configurecontainer

DependencyLifecycle mapping

DependencyLifecycle maps to ServiceLifetime as follows:

DependencyLifecycle Service Lifetime
InstancePerCall ServiceLifetime.Transient
InstancePerUnitOfWork ServiceLifetime.Scoped
SingleInstance ServiceLifetime.Singleton

Property injection

The NServiceBus.Extensions.DependencyInjection package does not support property injection out of the box. To enable property injection, refer to the configured container's documentation.

Externally managed mode

The package allows the container to be used in externally managed mode for full control of the dependency injection container via the EndpointWithExternallyManagedServiceProvider extension point:

snippet: externally-managed-mode

Warning

IServiceCollection and IServiceProvider instances must not be shared across mutliple NServiceBus endpoints to avoid conflicting registration that might cause incorrect behavior or runtime errors.