title: Using Autofac with Mvc controller constructor injection
body: <p>I'm having trouble with Autofac constructor injection. I'm somewhat solved my problem but I'm turning to the community for a full solution.</p>
  
  <p>This works with DI</p>
  
  <pre><code>builder.RegisterControllers(typeof(MvcApplication).Assembly)
         .InstancePerHttpRequest();
  </code></pre>
  
  <p>This does not.</p>
  
  <pre><code>builder.RegisterControllers(typeof(MvcApplication).Assembly)
         .AsImplementedInterfaces()
         .InstancePerHttpRequest();
  </code></pre>
  
  <p>Here's my controller.</p>
  
  <pre><code>public class HomeController : BaseController
  {
      public HomeController(IPlugin plugin)
      {
  
      }
  }
  </code></pre>
  
  <p>I have a module to resolve the IPlugin injection. I'm wondering why I need to take the <code>.AsImplementedInterfaces()</code> out to make this work. I would prefer to use interfaces because I'm using MEF to import IControllers from other assemblies at runtime.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks to japonex (see comments below) I've updated my solution. Here's what I did to get everything working.</p>
  
  <p>First my project has its own controllers, and I then use MEF to import controllers from other assemblies.</p>
  
  <p>Controllers for my current project must be registered without the <code>.AsImplementedInterfaces()</code> call, so like so</p>
  
  <pre><code>builder.RegisterControllers(typeof(MvcApplication).Assembly).InstancePerRequest();
  </code></pre>
  
  <p>This will put the controllers into Autofac using the type instead of as an interface (<code>MyProject.Controllers.HomeController</code> instead of <code>System.Web.Mvc.IController</code>).</p>
  
  <p>Next in my plugin project I simply had to export the types instead of as an interface.
  So I use this</p>
  
  <pre><code>[Export]
  [PartCreationPolicy(CreationPolicy.NonShared)]
  public class HomeController : PluginController
  </code></pre>
  
  <p>Instead of this</p>
  
  <pre><code>[Export(typeof(IController))]
  [PartCreationPolicy(CreationPolicy.NonShared)]
  public class HomeController : PluginController
  </code></pre>
  
  <p>Notice the difference in the <code>Export</code> attribute.</p>
  
  <p>Next I set the current <code>DependencyResolver</code> to the <code>AutofacDependencyResolver</code>.</p>
  
  <p>Last, but not least, I created a custom ControllerFactory to create the controllers. Technically this isn't needed, since I'm no longer using interfaces, but I have code to check the status of the controller before creating it. This allows me to easily enable/disable a plugin from an admin area. </p>
  
  <p>So the root of the problem is that Autofac needed types instead of interfaces to resolve properly. I'm sure this could be done with interfaces, but this solution works for me. The only real reason I would have wanted to use interfaces is because I could then ask for all controllers from Autofac without knowing their type.</p>
  
  <pre><code>public class MyControllerFactory : DefaultControllerFactory
  {
      private readonly IContainer _container;
  
      public PortalControllerFactory(IContainer container)
      {
          _container = container;
      }
  
      public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
      {
          Type controllerType = GetControllerType(requestContext, controllerName);
  
          if(controllerType == null)
          {
              throw new HttpException(404, ErrorMessages.Http404);
          }
  
          IPlugin plugin = PluginManager.Current.GetPlugin(controllerType);
  
          if (plugin != null &amp;&amp; plugin.Status != Common.Enums.PluginStatus.Enabled)
          {
              //the controller/plugin is disabled so modify the route data and return the controller
              RouteData data = new RouteData();
              data.Values.Add("controller", "plugin");
              data.Values.Add("action", "disabled");
              data.Values.Add("plugin", plugin.Name);
  
              requestContext.RouteData = data;
  
              return ViewRenderer.CreateController&lt;Project.Controllers.PluginController&gt;(data);
          }
  
          var controller = ((AutofacDependencyResolver)DependencyResolver.Current).RequestLifetimeScope.Resolve(controllerType);
          return controller as IController;
      }
  }
  </code></pre>