MVC Integration Changes in Autofac Beta 2.1.6
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));
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));