title: Inject dependencies in a WebAPI with Unity
body: <p>I have a WebAPI controller that (should) looks like this:</p>
  
  <pre><code>public class MyController : ApiController
  {
      private IRepository repository;
  
      public MyController(IRepository repository) 
      {
          this.repository = repositor;
      }
  
      // REST implementations
  }
  </code></pre>
  
  <p><code>WebApiConfig</code> is configured like that:</p>
  
  <pre><code>public static class WebApiConfig
  {
      public static void Register(HttpConfiguration config)
      {
          // Unity configuration
          var container = new UnityContainer();
          container
              .RegisterType&lt;IRepository , CustomRepository&gt;();
          config.DependencyResolver = new UnityResolver(container);
  
          // Web API routes
          // CORS
      }
  }
  </code></pre>
  
  <p>then in <code>Global.asax.cs</code> I have something like:</p>
  
  <pre><code>public class WebApiApplication : System.Web.HttpApplication
  {
      protected void Application_Start()
      {
          GlobalConfiguration.Configure(WebApiConfig.Register);
      }
  
      // CORS
      protected void Application_BeginRequest(object sender, EventArgs e)
      {
          // some CORS config
      } 
  }
  </code></pre>
  
  <p>and finally I have a <code>Startup.cs</code>:</p>
  
  <pre><code>[assembly: OwinStartup(typeof(Path.To.Startup))]
  namespace ZeroCode.ConfigurazioneServizio.Web.Api
  {
      public class Startup
      {
          public void Configuration(IAppBuilder app)
          {
              new Mapping().Bootstrap();
          }
      }
  }
  </code></pre>
  
  <p>To me everything looks ok, the code builds and I can launch the controller but as soon I make a request I get error cause parameterless constructor isn't present.
  So I've added the default constructor but this will instantiate the controller so <code>IRepository</code> will never be injected.</p>
  
  <p>I've searched for a possible solution. One of them tried to implement <code>IHttpControllerActivator</code> and so i've realized something like this:</p>
  
  <pre><code>public class UnityHttpControllerActivator : IHttpControllerActivator
  {
      private IUnityContainer _container;
  
      public UnityHttpControllerActivator(IUnityContainer container)
      {
          _container = container;
      }
  
      public IHttpController Create(System.Net.Http.HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
      {
          return (IHttpController)_container.Resolve(controllerType);
      }
  }
  </code></pre>
  
  <p>At this point I've modified the <code>WebApiConfig.cs</code> inserting this line</p>
  
  <pre><code>config.Services.Replace(typeof(IHttpControllerActivator), new UnityHttpControllerActivator(container));
  </code></pre>
  
  <p>right after the <code>config.DependencyResolver</code> but this doesn't resolve the issue and exception is raised inside the <code>Create</code> method. I don't know what else I can do.</p>