title: Cannot Inject Dependencies into ASP.NET Web API Controller using Unity
body: <p>Has anyone had any success running using an IoC container to inject dependencies into ASP.NET WebAPI controllers? I cannot seem to get it to work.</p>
  
  <p>This is what I'm doing now.  </p>
  
  <p>In my <code>global.ascx.cs</code>:</p>
  
  <p></p>
  
  <pre><code>    public static void RegisterRoutes(RouteCollection routes)
      {
              // code intentionally omitted 
      }
  
      protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
  
          RegisterGlobalFilters(GlobalFilters.Filters);
          RegisterRoutes(RouteTable.Routes);
  
          IUnityContainer container = BuildUnityContainer();
  
          System.Web.Http.GlobalConfiguration.Configuration.ServiceResolver.SetResolver(
              t =&gt;
              {
                  try
                  {
                      return container.Resolve(t);
                  }
                  catch (ResolutionFailedException)
                  {
                      return null;
                  }
              },
              t =&gt;
              {
                  try
                  {
                      return container.ResolveAll(t);
                  }
                  catch (ResolutionFailedException)
                  {
                      return new System.Collections.Generic.List&lt;object&gt;();
                  }
              });
  
          System.Web.Mvc.ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory(container)); 
  
          BundleTable.Bundles.RegisterTemplateBundles();
      }
  
      private static IUnityContainer BuildUnityContainer()
      {
          var container = new UnityContainer().LoadConfiguration();
  
          return container;
      }
  </code></pre>
  
  <p>My controller factory:</p>
  
  <p></p>
  
  <pre><code>public class UnityControllerFactory : DefaultControllerFactory
              {
                  private IUnityContainer _container;
  
                  public UnityControllerFactory(IUnityContainer container)
                  {
                      _container = container;
                  }
  
                  public override IController CreateController(System.Web.Routing.RequestContext requestContext,
                                                      string controllerName)
                  {
                      Type controllerType = base.GetControllerType(requestContext, controllerName);
  
                      return (IController)_container.Resolve(controllerType);
                  }
              }
  </code></pre>
  
  <p>It never seems to look in my unity file to resolve dependencies, and I get an error like:</p>
  
  <blockquote>
    <p>An error occurred when trying to create a controller of type
    'PersonalShopper.Services.WebApi.Controllers.ShoppingListController'.
    Make sure that the controller has a parameterless public constructor.</p>
    
    <p>at
    System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpControllerContext
    controllerContext, Type controllerType)
         at System.Web.Http.Dispatcher.DefaultHttpControllerFactory.CreateInstance(HttpControllerContext
    controllerContext, HttpControllerDescriptor controllerDescriptor)
         at System.Web.Http.Dispatcher.DefaultHttpControllerFactory.CreateController(HttpControllerContext
    controllerContext, String controllerName)
         at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage
    request, CancellationToken cancellationToken)
         at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage
    request, CancellationToken cancellationToken)</p>
  </blockquote>
  
  <p>Controller looks like:</p>
  
  <p></p>
  
  <pre><code>public class ShoppingListController : System.Web.Http.ApiController
      {
          private Repositories.IProductListRepository _ProductListRepository;
  
  
          public ShoppingListController(Repositories.IUserRepository userRepository,
              Repositories.IProductListRepository productListRepository)
          {
              _ProductListRepository = productListRepository;
          }
  }
  </code></pre>
  
  <p>My unity file looks like:</p>
  
  <pre><code>&lt;unity xmlns="http://schemas.microsoft.com/practices/2010/unity"&gt;
    &lt;container&gt;
      &lt;register type="PersonalShopper.Repositories.IProductListRepository, PersonalShopper.Repositories" mapTo="PersonalShopper.Implementations.MongoRepositories.ProductListRepository, PersonalShopper.Implementations" /&gt;
    &lt;/container&gt;
  &lt;/unity&gt;
  </code></pre>
  
  <p>Note that I don't have a registration for the controller itself because in previous versions of mvc the controller factory would figure out that the dependencies needed to be resolved.</p>
  
  <p>It seems like my controller factory is never being called.</p>