﻿# Autofac


> [!WARNING]
> Starting with NServiceBus version 7, the dependency injection container adapter packages are no longer required. NServiceBus directly supports the `Microsoft.Extensions.DependencyInjection` model and third party containers can be integrated using the [NServiceBus.Extensions.DependencyInjection package](/nservicebus/dependency-injection/extensions-dependencyinjection.md) or via the [.NET Generic Host](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host).
>
> In NServiceBus version 8, NServiceBus will no longer provide adapters for external dependency injection containers.
>
> Visit the [dependency injection upgrade guide](/nservicebus/upgrades/7to8/dependency-injection.md) for further information.


NServiceBus can be configured to use [Autofac](https://autofac.org/) for dependency injection.

## Default usage

<!-- snippet: Autofac -->

```cs
endpointConfiguration.UseContainer<AutofacBuilder>();
```

<!-- endsnippet -->

## Using an existing container

<!-- snippet: Autofac_Existing -->

```cs
var builder = new ContainerBuilder();
builder.RegisterInstance(new MyService());
var container = builder.Build();
endpointConfiguration.UseContainer<AutofacBuilder>(
    customizations: customizations =>
    {
        customizations.ExistingLifetimeScope(container);
    });
```

<!-- endsnippet -->

> [!WARNING]
> Although it is possible to update the container after passing it to NServiceBus using the `ContainerBuilder.Update` method, from Autofac 4.2.1 onwards that method [is marked as obsolete](https://github.com/autofac/Autofac/issues/811). It is recommended not to use this method to update the container after it has been passed to NServiceBus.

## DependencyLifecycle mapping

[`DependencyLifecycle`](/nservicebus/dependency-injection/index.md) maps to [Autofac instance scopes](https://docs.autofac.org/en/latest/lifetime/instance-scope.html#instance-scope) as follows:

| `DependencyLifecycle`                                                                                             | Autofac instance scope                                                                                                        |
|-----------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| [InstancePerCall](/nservicebus/dependency-injection/index.md) | [Instance Per Dependency](https://docs.autofac.org/en/latest/lifetime/instance-scope.html#instance-per-dependency)         |
| [InstancePerUnitOfWork](/nservicebus/dependency-injection/index.md)                    | [Instance Per Lifetime Scope](https://docs.autofac.org/en/latest/lifetime/instance-scope.html#instance-per-lifetime-scope) |
| [SingleInstance](/nservicebus/dependency-injection/index.md)                                  | [Single Instance](https://docs.autofac.org/en/latest/lifetime/instance-scope.html#single-instance)                          |


## Property Injection

This dependency injection container adapter automatically enables property injection for known types. Use the `Func` overload of `.ConfigureComponent` to get full control over the injected properties if needed.
