Autofac is very nice DI container, but documentation on their site is lacking on specifics.
This is how I set up Autofac in global.asax.cs:
and then do registration of all the things you want injected:
This is how I set up Autofac in global.asax.cs:
protected void Application_Start()
{
RegisterAutofac();
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
and then do registration of all the things you want injected:
private void RegisterAutofac()
{
var builder = new ContainerBuilder();
var config = GlobalConfiguration.Configuration;
// all controllers are going to be built by autofac
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// concrete implementation first, interface second
builder.RegisterType<ItemRepository>().As<IItemRepository>();
//
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}