Autofac je još jedan IoC kontejner za .Net, takođe vrlo popularan. Odlikuje ga manja veličina biblioteka u odnosu na ostale IoC kontejnere, ali ne i nedostatak funkcionalnosti.

Jednostavna implementacija IDependencyResolver interfejsa pomoću Autofac framework-a bi izgledala ovako:

public class AutofacDependencyResolver : IDependencyResolver
{
    private IContainer container;
 
    public AutofacDependencyResolver(Autofac.IContainer container)
    {
        this.container = container;
    }
 
    public object GetService(Type serviceType)
    {
        try
        {
            return this.container.Resolve(serviceType);
        }
        catch
        {
            return null;
        }
    }
 
    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            Type type = typeof(IEnumerable).MakeGenericType(
            new Type[]
            {
                serviceType
            });
            object obj = this.container.Resolve(type);
            return (IEnumerable<object>)obj;
        }
        catch
        {
            return new List<object>();
        }
    }
}

Setup Autofac IoC kontejnera treba staviti u Global.asax.cs fajl:

protected void Application_Start()
{
    RegisterAutofacDependencyResolver();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}
 
private void RegisterAutofacDependencyResolver()
{
    var builder = new ContainerBuilder();
    builder.RegisterType<HomeController>();
    builder.RegisterType<ConcreteRepository>().As<IRepository>();
 
    DependencyResolver.SetResolver(
    new AutofacDependencyResolver(builder.Build()));
}