• Stars
    star
    171
  • Rank 222,266 (Top 5 %)
  • Language
    C#
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Autofac implementation of the interfaces in Microsoft.Extensions.DependencyInjection.Abstractions, the .NET Core dependency injection abstraction.

Autofac.Extensions.DependencyInjection

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 => {
        webHostBuilder
          .UseContentRoot(Directory.GetCurrentDirectory())
          .UseIISIntegration()
          .UseStartup<Startup>()
      })
      .Build();

    await host.RunAsync();
  }
}

public class Startup
{
  public Startup(IWebHostEnvironment env)
  {
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    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.
    services.AddOptions();
  }

  // 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)
  {
      loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
      loggerFactory.AddDebug();
      app.UseMvc();
  }
}

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.

More Repositories

1

Autofac

An addictive .NET IoC container
C#
4,256
star
2

Examples

Example projects that consume and demonstrate Autofac IoC functionality and integration
C#
379
star
3

Autofac.Extras.DynamicProxy

Interceptor and decorator support for Autofac IoC via Castle DynamicProxy
C#
102
star
4

Autofac.AspNetCore.Multitenant

Enables multitenant dependency injection support for ASP.NET Core.
C#
101
star
5

Documentation

Usage and API documentation for Autofac and integration libraries
JavaScript
66
star
6

Autofac.Mvc

ASP.NET MVC integration for Autofac
C#
47
star
7

Autofac.Configuration

Configuration support for Autofac IoC
C#
36
star
8

Autofac.Extras.Moq

Moq auto mocking integration for Autofac IoC
C#
35
star
9

Autofac.Multitenant

Multitenant application support for Autofac IoC
C#
35
star
10

Autofac.WebApi

ASP.NET Web API integration for Autofac
C#
35
star
11

Autofac.ServiceFabric

Autofac integration for Azure Service Fabric. Provides service factory implementations for Actors, Stateful Services and Stateless Services.
C#
25
star
12

Autofac.Owin

OWIN integration for Autofac
C#
22
star
13

Autofac.Wcf

Windows Communication Foundation (WCF) integration for Autofac IoC
C#
20
star
14

Autofac.Mef

Managed Extensibility Framework (MEF) integration for Autofac IoC
C#
19
star
15

Autofac.Web

ASP.NET WebForms integration for Autofac
C#
10
star
16

Autofac.SignalR

SignalR integration for Autofac IoC
PowerShell
10
star
17

Autofac.AspNetCore

Autofac extensions and helpers for ASP.NET Core
PowerShell
9
star
18

Autofac.WebApi.Owin

OWIN support for the ASP.NET Web API integration for Autofac
C#
8
star
19

Autofac.Extras.CommonServiceLocator

Common Service Locator implementation for Autofac IoC
PowerShell
7
star
20

Autofac.Extras.FakeItEasy

FakeItEasy auto mocking integration for Autofac IoC
C#
7
star
21

Autofac.Extras.NHibernate

Autofac implementation of the NHibernate factories
C#
6
star
22

Autofac.Extras.MvvmCross

MvvmCross integration for Autofac IoC
C#
5
star
23

Autofac.Extras.AggregateService

Dynamic aggregate service implementation generation for Autofac IoC
C#
4
star
24

Autofac.Multitenant.Wcf

Multitenant Windows Communication Foundation (WCF) enhancements for Autofac IoC
C#
3
star
25

Autofac.Pooling

Support for pooled instance lifetime scopes in Autofac dependency injection.
C#
3
star
26

Autofac.Extensions.Hosting

Fluent configuration of Autofac with the Microsoft.Extensions.Hosting package
PowerShell
3
star
27

Autofac.Extras.AttributeMetadata

Attribute metadata support for Autofac IoC
C#
3
star
28

Autofac.Bot.Api

Handlers for Autofac Bot commands that can be used to execute various Autofac tasks like running benchmarks.
C#
2
star
29

Autofac.Mvc.Owin

OWIN support for the ASP.NET MVC integration for Autofac
PowerShell
2
star
30

Autofac.Diagnostics.DotGraph

Autofac diagnostics support to enable DOT graph visualization of resolve requests.
C#
2
star
31

autofac-bot

GitHub application based on Probot for executing common Autofac-based tasks.
TypeScript
1
star
32

Autofac.Analyzers

Roslyn code analyzers to help with Autofac usage.
C#
1
star
33

Autofac.Extras.DomainServices

Autofac Domain Service Factory for RIA Services
C#
1
star