MVC
Autofac is always kept up to date to support the latest version of ASP.NET MVC, so documentation is also kept up with the latest. Generally speaking, the integration remains fairly consistent across versions.
MVC integration requires the Autofac.Mvc5 NuGet package.
MVC integration provides dependency injection integration for controllers, model binders, action filters, and views. It also adds per-request lifetime support.
This page explains ASP.NET classic MVC integration. If you are using ASP.NET Core, see the ASP.NET Core integration page.
- Quick Start
- Register Controllers
- Set the Dependency Resolver
- Register Model Binders
- Register Web Abstractions
- Enable Property Injection for View Pages
- Enable Property Injection for Action Filters
- Enable Injection of Action Parameters
- OWIN Integration
- Using “Plugin” Assemblies
- Using the Current Autofac DependencyResolver
- Glimpse Integration
- Unit Testing
- Example
Quick Start
To get Autofac integrated with MVC you need to reference the MVC integration NuGet package, register your controllers, and set the dependency resolver. You can optionally enable other features as well.
protected void Application_Start()
{
var builder = new ContainerBuilder();
// Register your MVC controllers. (MvcApplication is the name of
// the class in Global.asax.)
builder.RegisterControllers(typeof(MvcApplication).Assembly);
// OPTIONAL: Register model binders that require DI.
builder.RegisterModelBinders(typeof(MvcApplication).Assembly);
builder.RegisterModelBinderProvider();
// OPTIONAL: Register web abstractions like HttpContextBase.
builder.RegisterModule<AutofacWebTypesModule>();
// OPTIONAL: Enable property injection in view pages.
builder.RegisterSource(new ViewRegistrationSource());
// OPTIONAL: Enable property injection into action filters.
builder.RegisterFilterProvider();
// OPTIONAL: Enable action method parameter injection (RARE).
builder.InjectActionInvoker();
// Set the dependency resolver to be Autofac.
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
The sections below go into further detail about what each of these features do and how to use them.
Register Controllers
At application startup, while building your Autofac container, you should register your MVC controllers and their dependencies. This typically happens in an OWIN startup class or in the Application_Start
method in Global.asax
.
var builder = new ContainerBuilder();
// You can register controllers all at once using assembly scanning...
builder.RegisterControllers(typeof(MvcApplication).Assembly);
// ...or you can register individual controlllers manually.
builder.RegisterType<HomeController>().InstancePerRequest();
Note that ASP.NET MVC requests controllers by their concrete types, so registering them As<IController>()
is incorrect. Also, if you register controllers manually and choose to specify lifetimes, you must register them as InstancePerDependency()
or InstancePerRequest()
- ASP.NET MVC will throw an exception if you try to reuse a controller instance for multiple requests.
Set the Dependency Resolver
After building your container pass it into a new instance of the AutofacDependencyResolver
class. Use the static DependencyResolver.SetResolver
method to let ASP.NET MVC know that it should locate services using the AutofacDependencyResolver
. This is Autofac’s implementation of the IDependencyResolver
interface.
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));