My favorites | Sign in
Logo
                
Search
for
Updated Mar 21, 2009 by vijay.santhanam
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

Your ASP.NET MVC web application must reference Autofac.dll and Autofac.Integration.Web.dll.

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.


Sign in to add a comment
Hosted by Google Code