My favorites | Sign in
Logo
                
Search
for
Updated Oct 21, 2009 by nicholas.blumhardt
Labels: Featured
MvcIntegration  
Autofac.Integration.Mvc provides an IControllerFactory implementation for System.Web.Mvc (v1.0)

Getting the Code

To use ASP.NET MVC you need to install the release from http://asp.net/mvc/.

Project Integration

Referenced Assemblies

For ASP.NET MVC to work with Autofac, both Autofac.dll and Autofac.Integration.Web.dll must be referenced by the application or placed in the /Bin directory.

Web.config

The next step is to add the Autofac disposer module to the Web.config file:

<configuration>
  <system.web>
    <httpModules>
      <add name="ContainerDisposal" type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"/>

The Container Disposal Module lets Autofac dispose of any components created during request processing as soon as the request completes. The Property Injection Module injects dependencies into pages before the page lifecycle executes. (An alternative Unset Property Injection module is also provided.)

IIS 7+ Note: The Autofac configuration may need to appear in the modules section in order to work in integrated (vs. classic) pipeline mode. See http://groups.google.com/group/autofac/browse_thread/thread/6331896f85e685b1.

Global.asax

The HttpApplication instance must support IContainerProviderAccessor. The relevant sections of a global application class are shown below:

public class Global : HttpApplication, IContainerProviderAccessor
{
  static IContainerProvider _containerProvider;

  protected void Application_Start(object sender, EventArgs e)
  {
    var builder = new ContainerBuilder();

    builder.RegisterModule(new AutofacControllerModule(Assembly.GetExecutingAssembly()));

    _containerProvider = new ContainerProvider(builder.Build());

    ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));

    RegisterRoutes(RouteTable.Routes);
  }

  public IContainerProvider ContainerProvider
  {
    get { return _containerProvider; }
  }

  protected void Application_EndRequest(object sender, EventArgs e)
  {
    ContainerProvider.DisposeRequestContainer();
  }

  static void RegisterRoutes(RouteCollection routes) { } // See MVC documentation
}

Notes on IContainerProvider

IContainerProvider exposes two useful properties: ApplicationContainer and RequestContainer. The first is the root container that was built at application start-up. The second, RequestContainer should be used whenever manual dependency resolution/service lookup is required. The components that it contains (apart from any singletons) will be specific to the current request.

Registering Controllers

You can register controllers by hand, using named registrations:

builder.Register<HomeController>().Named("controller.home").FactoryScoped();

The Autofac integration will convert all controller names to lowercase before retrieving them from the container (you can customise this through IControllerIdentificationStrategy.)

An easier way to register controllers is using AutofacControllerModule, which searches the provided assemblies for implementations of IController:

builder.RegisterModule(new AutofacControllerModule(Assembly.GetExecutingAssembly()));

HTTP Request Scope

HttpRequestScoped() means that at most one instance of the component will be created for each incoming web request. This is handy for items that should be shared within a single request, e.g. repositories.

Installing into ControllerBuilder

The AutofacControllerFactory class ties the container provider into the ASP.NET MVC ControllerBuilder (in the Application_Start() method of the app class):

ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(_containerProvider));

Comment by jake.net, Jun 01, 2008

Just wondering how hard it would be to update this to Preview 3 of asp.net mvc

Comment by nicholas.blumhardt, Oct 03, 2008

Hi Jake - should be working with the most recent release now.

Comment by awhisperedlie, Oct 21, 2009

This page should also mention that the web.config will need to be updated, as stated for ASPNET WebForms?, for those users that skip right to this MVC page.

<configuration>
  <system.web>
    <httpModules>
      <add name="ContainerDisposal" type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"/>
      <add name="PropertyInjection" type="Autofac.Integration.Web.PropertyInjectionModule, Autofac.Integration.Web"/>
Comment by nicholas.blumhardt, Oct 21, 2009

Thanks for the suggestion, I've updated the article!


Sign in to add a comment
Hosted by Google Code