Wire ASP.NET 4 WEB API RTM with Autofac

It took me a little while to get this working but here is how I got Autofac working with the new ASP.NET MVC 4 Web API.

Call this setup method in Application Start event in global asax.

private static void SetupAutofac()
{
var builder = new ContainerBuilder();
builder.RegisterModule(new MessagingModule());
builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
var container = builder.Build();
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

This sets up all API and regular controllers while injecting all the properties in them. The module (MessaagingModule) sets up the Autofac container.