Autofac 1.4 and earlier 2.1 versions used AutofacControllerModule as a way of finding and registering ASP.NET MVC controllers with the container.
In the Autofac 2.1 MVC integration, there is a new ContainerBuilder extension method called RegisterControllers.
var builder = new ContainerBuilder();
// Was:
// builder.RegisterModule(new AutofacControllerModule(...))
builder.RegisterControllers(Assembly.GetExecutingAssembly());
_containerProvider = new ContainerProvider(builder.Build());
ControllerBuilder.Current.SetControllerFactory(
new AutofacControllerFactory(_containerProvider));
// Was:
// builder.RegisterModule(new AutofacControllerModule(...))
builder.RegisterControllers(Assembly.GetExecutingAssembly());
_containerProvider = new ContainerProvider(builder.Build());
ControllerBuilder.Current.SetControllerFactory(
new AutofacControllerFactory(_containerProvider));
The reason for the switch is that RegisterControllers is a thin wrapper around the new assembly scanner, and supports the same syntax as the rest of Autofac’s registration methods:
builder.RegisterControllers(assembly)
.InjectProperties()
.OnActivated(e => Log.Information("Controller created: " + e.Instance));
.InjectProperties()
.OnActivated(e => Log.Information("Controller created: " + e.Instance));
[...] MVC Integration Changes in Autofac Beta 2.1.6 (Nicholas Blumhardt) [...]
Update – in the RTW build of Autofac 2, you now need to reference Autofac.Integration.Web.Mvc.dll as well as Autofac.Integration.Web.dll.
[...] ASP.NET MVC的Controller注册更灵活和更简单,通过方法RegisterControllers() 进行注册,参看文章MVC Integration Changes in Autofac Beta 2.1.6. [...]