title: How to use IDispatchMessageInspector in a WCF Service?
body: <p>I am trying to use <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageinspector.aspx" rel="noreferrer">IDispatchMessageInspector</a> in a WCF service implementation to access custom header values.</p>
  
  <p>Something like:</p>
  
  <pre><code>public class MyService : IMyService
  {
      public List&lt;string&gt; GetNames()
      {
          var headerInspector = new CustomHeaderInspector();
  
          // Where do request &amp; client channel come from?
          var values = headerInspector.AfterReceiveRequest(ref request, clientChannel, OperationContext.Current.InstanceContext);            
      }
  }
  </code></pre>
  
  <p>I've implemented my own IDispatchMessageInspector class.</p>
  
  <pre><code>public class CustomHeaderInspector : IDispatchMessageInspector
  {
      public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
      {
          var prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
          var userName = prop.Headers["Username"];
  
          return userName;
      }
  }
  </code></pre>
  
  <p>How do I pass</p>
  
  <ul>
  <li><p>System.ServiceModel.Channels.<strong>Message</strong> and</p></li>
  <li><p>System.ServiceModel.<strong>IClientChannel</strong></p></li>
  </ul>
  
  <p>to <strong>AfterReceiveRequest</strong> called <em>from the service implementation?</em></p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Many articles like <a href="https://web.archive.org/web/20130806230959/http://msmvps.com/blogs/paulomorgado/archive/2007/04/27/wcf-building-an-http-user-agent-message-inspector.aspx" rel="noreferrer">this one</a> or <a href="http://blogs.msdn.com/b/zelmalki/archive/2008/12/29/creating-a-wcf-idispatchmessageinspector.aspx" rel="noreferrer">this one</a>, give examples on how to implement your own <code>ServiceBehavior</code>. So your service implementation looks like this:</p>
  
  <pre><code>[MyCustomBehavior]
  public class MyService : IMyService
  {
      public List&lt;string&gt; GetNames()
      {
          // Can you use 'MyCustomBehavior' here to access the header properties?
      }
  }
  </code></pre>
  
  <p>So with this, can I access <code>MyCustomBehavior</code> somehow within the service operation method to access custom header values?</p>