Autofac.Extensions.DependencyInjection 9.0.0


Autofac is an IoC container for Microsoft .NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .NET classes as components.

Build status codecov Open in Visual Studio Code

Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.

Get Started in ASP.NET Core

This quick start shows how to use the IServiceProviderFactory{T} integration that ASP.NET Core supports to help automatically build the root service provider for you. If you want more manual control, check out the documentation for examples.

  • Reference the Autofac.Extensions.DependencyInjection package from NuGet.
  • In your Program.Main method, where you configure the HostBuilder, call UseAutofac to hook Autofac into the startup pipeline.
  • In the ConfigureServices method of your Startup class register things into the IServiceCollection using extension methods provided by other libraries.
  • In the ConfigureContainer method of your Startup class register things directly into an Autofac ContainerBuilder.

The IServiceProvider will automatically be created for you, so there's nothing you have to do but register things.

public class Program
  public static async Task Main(string[] args)
    // The service provider factory used here allows for
    // ConfigureContainer to be supported in Startup with
    // a strongly-typed ContainerBuilder.
    var host = Host.CreateDefaultBuilder(args)
      .UseServiceProviderFactory(new AutofacServiceProviderFactory())
      .ConfigureWebHostDefaults(webHostBuilder => {

    await host.RunAsync();

public class Startup
  public Startup(IWebHostEnvironment env)
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
    this.Configuration = builder.Build();

  public IConfiguration Configuration { get; private set; }

  // ConfigureServices is where you register dependencies. This gets
  // called by the runtime before the ConfigureContainer method, below.
  public void ConfigureServices(IServiceCollection services)
    // Add services to the collection. Don't build or return
    // any IServiceProvider or the ConfigureContainer method
    // won't get called.

  // ConfigureContainer is where you can register things directly
  // with Autofac. This runs after ConfigureServices so the things
  // here will override registrations made in ConfigureServices.
  // Don't build the container; that gets done for you. If you
  // need a reference to the container, you need to use the
  // "Without ConfigureContainer" mechanism shown later.
  public void ConfigureContainer(ContainerBuilder builder)
      builder.RegisterModule(new AutofacModule());

  // Configure is where you add middleware. This is called after
  // ConfigureContainer. You can use IApplicationBuilder.ApplicationServices
  // here if you need to resolve things from the container.
  public void Configure(
    IApplicationBuilder app,
    ILoggerFactory loggerFactory)

Our ASP.NET Core integration documentation contains more information about using Autofac with ASP.NET Core.

Get Help

Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.

No packages depend on Autofac.Extensions.DependencyInjection.

Release notes are at

Version Downloads Last updated
9.0.0 10 01/23/2024
8.0.0 47 06/27/2022
7.2.0 43 06/27/2022
7.2.0-preview.1 18 08/12/2022
7.1.0 24 07/03/2022
7.0.2 39 08/12/2022
7.0.1 41 08/12/2022
7.0.0 20 08/12/2022
6.0.0 22 08/12/2022
5.0.1 25 08/12/2022
5.0.0 36 08/12/2022
5.0.0-rc2 43 08/12/2022
5.0.0-rc1 47 08/12/2022
4.4.0 44 07/02/2022
4.3.1 22 06/27/2022
4.3.0 33 08/12/2022
4.2.2 48 08/12/2022
4.2.1 19 08/12/2022
4.2.0 20 06/27/2022
4.1.0 35 08/12/2022
4.0.0 23 08/12/2022
4.0.0-rc3-309 23 08/12/2022
4.0.0-rc3-280 43 06/27/2022
4.0.0-rc2-240 37 08/12/2022
4.0.0-rc1-177 34 06/28/2022