title: Configuring dependency injection with ASP.NET Web API 2.1
body: <p>I'm creating an ASP.NET Web API 2.1 site and as I want to inject dependencies directly into the controllers, I've created my own implementation of IDependencyResolver so that StructureMap will handle that for me.</p>
  
  <pre><code>public class StructureMapDependencyResolver : IDependencyResolver
  {
      public IDependencyScope BeginScope()
      {
          return this;
      }
  
      public object GetService(Type serviceType)
      {
          return ObjectFactory.GetInstance(serviceType);
      }
  
      public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {            
          return ObjectFactory.GetAllInstances(serviceType).Cast&lt;object&gt;();
      }
  
      public void Dispose()
      {
      }
  }
  </code></pre>
  
  <p>I've then told Web API to use this class by adding this line to the Application_Start method in Global.asax</p>
  
  <pre><code>GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver();
  </code></pre>
  
  <p>That compiled but when I tried to access any of the API methods in a browser I got an error like this</p>
  
  <pre><code>No Default Instance defined for PluginFamily System.Web.Http.Hosting.IHostBufferPolicySelector, System.Web.Http
  </code></pre>
  
  <p>That one was relatively easy to solve as I added a line to my StructureMap configuration</p>
  
  <pre><code>this.For&lt;IHostBufferPolicySelector&gt;().Use&lt;WebHostBufferPolicySelector&gt;();
  </code></pre>
  
  <p>However then I got other similar errors for other System.Web.Http classes and while I could resolve some of them I am stuck on how to deal with 3 of them, namely ITraceManager, IExceptionHandler and IContentNegotiator.</p>
  
  <p>The issue is that TraceManager which seems to be the default implementation of ITraceManager is an internal class and so I can't reference it in my StructureMap configuration.</p>
  
  <p>So am I going about this completely the wrong way or is there some other way to inject these internal classes?</p>