添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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.

Leave a comment

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Email Address: