﻿# NServiceBus.Extensions.DependencyInjection


> [!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](/nservicebus/dependency-injection/index.md#modes-of-operation-externally-managed-mode). Visit the [dependency injection upgrade guide](/nservicebus/upgrades/7to8/dependency-injection.md) 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](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host) to manage application and dependency injection container lifecycle. Use the [NServiceBus.Extensions.Hosting](/nservicebus/hosting/extensions-hosting.md) 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 -->

```cs
endpointConfiguration.UseContainer(new DefaultServiceProviderFactory());
```

<!-- endsnippet -->


## 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 -->

```cs
endpointConfiguration.UseContainer(new AutofacServiceProviderFactory(containerBuilder =>
{
    containerBuilder.RegisterInstance(new MyService());
}));
```

<!-- endsnippet -->

### Castle Windsor

<!-- snippet: CastleUsage -->

```cs
var containerSettings = endpointConfiguration.UseContainer(new WindsorServiceProviderFactory());

containerSettings.ConfigureContainer(c => c.Register(Component.For<MyService>().Instance(new MyService())));
```

<!-- endsnippet -->

### StructureMap

<!-- snippet: StructureMapUsage -->

```cs
var registry = new Registry();
registry.For<MyService>().Use(new MyService());

endpointConfiguration.UseContainer(new StructureMapServiceProviderFactory(registry));
```

<!-- endsnippet -->

### Unity

<!-- snippet: UnityUsage -->

```cs
var container = new UnityContainer();
container.RegisterInstance(new MyService());

endpointConfiguration.UseContainer<IUnityContainer>(new ServiceProviderFactory(container));
```

<!-- endsnippet -->


## 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 -->

```cs
var containerSettings = endpointConfiguration.UseContainer(new DefaultServiceProviderFactory());
containerSettings.ServiceCollection.AddSingleton<MyService>();
```

<!-- endsnippet -->


### ContainerBuilder

Third-party container-native APIs can be accessed by with `ConfigureContainer`.

<!-- snippet: settings-configurecontainer -->

```cs
// use Autofac:
var containerSettings = endpointConfiguration.UseContainer(new AutofacServiceProviderFactory());
containerSettings.ConfigureContainer(containerBuilder =>
{
    // access Autofac native APIs here:
    containerBuilder.RegisterType<MyService>().AsSelf().SingleInstance();
    containerBuilder.RegisterAssemblyModules(Assembly.GetCallingAssembly());
});
```

<!-- endsnippet -->


## DependencyLifecycle mapping

[`DependencyLifecycle`](/nservicebus/dependency-injection/index.md) maps to [ServiceLifetime](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicelifetime) as follows:

| `DependencyLifecycle`                                                                                             | Service Lifetime                                                                                                        |
|-----------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| [InstancePerCall](/nservicebus/dependency-injection/index.md) | [ServiceLifetime.Transient](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicelifetime)         |
| [InstancePerUnitOfWork](/nservicebus/dependency-injection/index.md)                    | [ServiceLifetime.Scoped](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicelifetime) |
| [SingleInstance](/nservicebus/dependency-injection/index.md)                                  | [ServiceLifetime.Singleton](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicelifetime)                          |

## 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](/nservicebus/dependency-injection/index.md#modes-of-operation-externally-managed-mode) for full control of the dependency injection container via the `EndpointWithExternallyManagedServiceProvider` extension point:

<!-- snippet: externally-managed-mode -->

```cs
var serviceCollection = new ServiceCollection();

serviceCollection.AddTransient<MyService>();

var startableEndpoint = EndpointWithExternallyManagedServiceProvider.Create(endpointConfiguration, serviceCollection);

var serviceProvider = serviceCollection.BuildServiceProvider();

var endpoint = await startableEndpoint.Start(serviceProvider);

serviceProvider.GetService<MyService>();
```

<!-- endsnippet -->

> [!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.
