title: How to obtain header values from Client Message Inspector in WCF
body: <p>I'm creating a web test client for some services I'm working on, and as part of the requirements for that, I would like to be able to display the full request and response SOAP messages (and HTTP headers to the user).</p>
  
  <p>I implemented a MessageInspector class implementing <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageinspector.aspx" rel="nofollow">IClientMessageInspector</a>, most notably the <code>BeforeSendRequest</code> and <code>AfterReceiveReply</code> methods to get access to the request &amp; response messages respectively.</p>
  
  <p>Capturing the response (AfterReceiveReply) works great, but capturing the request only partially works.  I can access most of the message, however the SOAP header and HTTP headers are both empty.  Viewing the request in Fiddler, I can see that WCF is sending a Security header in the SOAP message and "a bunch" of HTTP headers.</p>
  
  <p>My <code>BeforeSendRequest</code> method is very simple...the gist of it is...</p>
  
  <pre><code>public object BeforeSendRequest(ref Message request, IClientChannel channel)
  {  
      this.RequestMessage = request.ToString(); // Security header is missing from message
  
      // Try to get HTTP headers
      object req; // req is always null
      if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out req))
      {
          this.RequestHeaders = ((HttpRequestMessageProperty)req).Headers;
      }
  
      return null;
  }
  </code></pre>
  
  <p>I'm not sure why the HTTP and Security headers are missing.  Is there a better way to do this?</p>
  
  <ul>
  <li>Wayne</li>
  </ul>