Autofac is an addictive Inversion of Control container for .NET Core, ASP.NET Core, .NET 4.5.1+, Universal Windows apps, and more.

Register Components

Build up containers with lambdas, types, or pre-built instances of components. You can also scan assemblies for registrations.

var builder = new ContainerBuilder();

// Register individual components
builder.RegisterInstance(new TaskRepository())
       .As<ITaskRepository>();
builder.RegisterType<TaskController>();
builder.Register(c => new LogManager(DateTime.Now))
       .As<ILogger>();

// Scan an assembly for components
builder.RegisterAssemblyTypes(myAssembly)
       .Where(t => t.Name.EndsWith("Repository"))
       .AsImplementedInterfaces();

var container = builder.Build();

Express Dependencies

Let Autofac inject your constructor parameters for you. It can also handle property and method injection.

public class TaskController
{
  private ITaskRepository _repository;
  private ILogger _logger;

  // Autofac will automatically find the registered
  // values and pass them in for you.
  public TaskController(
    ITaskRepository repository,
    ILogger logger)
  {
    this._repository = repository;
    this._logger = logger;
  }
}

Flexible Module System

Strike a balance between the deployment-time benefits of XML configuration and the power of code with Autofac modules.

// Specify complex registrations in code
public class CarTransportModule : Module
{
  public bool ObeySpeedLimit { get; set; }

  protected override void Load(ContainerBuilder builder)
  {
    builder.RegisterType<Car>().As<IVehicle>();

    if (ObeySpeedLimit)
      builder.RegisterType<SaneDriver>().As<IDriver>();
    else
      builder.RegisterType<CrazyDriver>().As<IDriver>();
  }
}
<!-- Change deployment-time behavior with XML -->
<autofac>
  <module type="CarTransportModule">
    <properties>
      <property name="ObeySpeedLimit" value="true" />
    </properties>
  </module>
</autofac>

Simple Extension Points

Autofac provides activation events to let you know when components are being activated or released, allowing for a lot of customization with little code.

var builder = new ContainerBuilder();

// Once a listener has been fully constructed and is
// ready to be used, automatically start listening.
builder.RegisterType<Listener>()
       .As<IListener>()
       .OnActivated(e => e.Instance.StartListening());

// When a processor is being constructed but before
// it's ready to be used, call an initialization method.
builder.RegisterType<Processor>()
       .OnActivating(e => e.Instance.Initialize());

var container = builder.Build();

Dive In

Download

Download

The easiest way to get Autofac is through NuGet. Here are the Autofac packages in the NuGet Gallery.

Learn

Learn

If you're new to Autofac, the Quick Start guide is a good place to start. There's also an official documentation site, API documentation, and lots of info on the Autofac wiki. For questions, hit us up on StackOverflow.

Get Involved

Get Involved

Found an issue? Let us know! Want to help us improve Autofac? Check out the source and our contributor's guide, and drop us a line on the discussion forum!