title: Capturing and injecting HttpRequestMessage in Web API with Ninject
body: <p>I've got a class that requires access to the HttpRequestMessage in my Web API service. At the moment, I've got the following code to capture the message in the pipeline and save it for later (based on <a href="http://blog.ploeh.dk/2012/04/19/WiringHttpControllerContextWithCastleWindsor/" rel="nofollow">this</a> and <a href="http://blog.ploeh.dk/2012/10/03/DependencyInjectioninASP.NETWebAPIwithCastleWindsor/" rel="nofollow">this</a>):</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class ContextCapturingControllerActivator : IHttpControllerActivator
  {
      private readonly IKernel kernel;
      private HttpRequestMessage requestMessage;
  
      public ContextCapturingControllerActivator(IKernel kernel)
      {
          this.kernel = kernel;
      }
  
      public IHttpController Create(HttpRequestMessage requestMessage, 
                                    HttpControllerDescriptor controllerDescriptor, 
                                    Type controllerType)
      {
          this.kernel.Rebind&lt;HttpRequestMessage&gt;()
              .ToConstant&lt;HttpRequestMessage&gt;(requestMessage);
  
          var controller = (IHttpController)this.kernel.GetService(controllerType);
  
          this.requestMessage = requestMessage;
          requestMessage.RegisterForDispose(
              new Release(() =&gt; this.kernel.Release(controller)));
  
          return controller; 
      }
  
      private class Release : IDisposable
      {
          private readonly Action release;
  
          public Release(Action release)
          {
              this.release = release;
          }
  
          public void Dispose()
          {
              this.release();
          }
      }
  }
  </code></pre>
  
  <p>In my composition root, I configure the <code>ControllerActivator</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>kernel.Bind&lt;IHttpControllerActivator&gt;()
        .To&lt;ContextCapturingControllerActivator&gt;();
  </code></pre>
  
  <p>The end result is that from the perspective of the configuration, the <code>HttpRequestMessage</code> is "magically" injected wherever it is requested since it is done for us inside the <code>ControllerActivator</code>. I have not been able to inject the message from my composition root. I'm also not crazy about the Rebind since it's there to avoid adding a new binding every time the service is called. I suspect it's due to the singleton nature of the Web API stack, but have not been able to sort out how to deal with that properly.</p>
  
  <p>In general, I cannot use the latest unstable Nuget package of Ninject web api due to the error reported (and ignored) <a href="https://groups.google.com/forum/#!msg/ninject/rC2vhj8yvBU/NAIkNA-QrAAJ" rel="nofollow">here</a>.</p>
  
  <p>Can anyone suggest the proper way to improve my code to make it a bit more clear and make life easier for future maintainers (and let's face it -- that's probably going to be me).</p>
  
  <p>Thanks.</p>