title: using a Handler in Web API and having Unity resolve per request
body: <p>I am using Unity as my IoC framework and I am creating a type based on the value in the header of each request in a handler:</p>
  
  <pre><code>var container = new UnityContainer();
  
  container.RegisterType&lt;IFoo,Foo&gt;(new InjectionConstructor(valuefromHeader));
  
  GlobalConfiguration.Configuration.DependencyResolver = 
      new Unity.WebApi.UnityDependencyResolver(container);
  </code></pre>
  
  <p>The problem is that the handler's <code>SendAsync</code> means that the global container is getting overwritten by different requests and the controllers that use IFoo in their constructor are getting the wrong values.  </p>
  
  <p>1) Can I make the SendAsync sync?
  2) If not, how do I create different instances for each request and have the IoC container resolve safely?</p>
  
  <p>I have looked at the following articles without success:</p>
  
  <p><a href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver" rel="noreferrer">http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver</a>
  <a href="http://www.strathweb.com/2012/11/asp-net-web-api-and-dependencies-in-request-scope/" rel="noreferrer">http://www.strathweb.com/2012/11/asp-net-web-api-and-dependencies-in-request-scope/</a>
  <a href="http://benfoster.io/blog/per-request-dependencies-in-aspnet-web-api-using-structuremap" rel="noreferrer">http://benfoster.io/blog/per-request-dependencies-in-aspnet-web-api-using-structuremap</a></p>
  
  <p>Thanks in advance.</p>