title: Getting Security Headers into a WCF service with custom message/formatter in .NET 3.0
body: <p>We've inherited a WCF web service that has a custom MessageFormatter that constructs a custom Message subclass in the SerializeReply Method.</p>
  
  <pre><code>class OurMessageFormatter : MessageFormatter
  {
    public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
    {
      OurResponse ourResponse = (OurResponse) result;
  
      // some validation here...
  
      OurMessage reply = new OurMessage(ourResponse, MessageVersion.Soap11);
      return reply;
    }
  
  }
  </code></pre>
  
  <p>The problem we're facing is that the custom Message subclass wouldn't have any headers populated. We tried to see if WCF would populate the generic ones (MessageID, ResponseTo, Action and such) out of the box, but no luck. Then we realized that the custom Message subclass had implemented the Headers Property like so...</p>
  
  <pre><code>class OurMessage : Message
  {
    public override MessageHeaders Headers
    {
      get { return new MessageHeaders(MessageVersion.Soap11WSAddressing10); }
    }
  }
  </code></pre>
  
  <p>... lotta help that turned out to be! So we rewrote it as so...</p>
  
  <pre><code>class OurMessage : Message
  {
    MessageHeaders headers;
  
    public OurMessage()
    {
      // ...
  
      headers = new MessageHeaders(MessageVersion.Soap11WSAddressing10);
    }
  
    public override MessageHeaders Headers
    {
      get { return headers; }
    }
  }
  </code></pre>
  
  <p>... and still no luck.</p>
  
  <p>So we went on to hand code the headers; first in the Formatter...</p>
  
  <pre><code>class OurMessageFormatter : MessageFormatter
  {
  
    public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
    {
      //...
  
      OurMessage reply = new OurMessage(ourResponse, MessageVersion.Soap11);
      ourMessage.MessageID = ...;
      ourMessage.ResponseTo = ...;
      ourMessage.Action = ...;
      // more headers set ...
  
      return reply;
    }
  
  }
  </code></pre>
  
  <p>... and then in the Message itself...</p>
  
  <pre><code>class OurMessage : Message
  {
    public override MessageHeaders Headers
    {
      get
      {
        MessageHeaders headers = new MessageHeaders(MessageVersion.Soap11WSAddressing10);
        ourMessage.MessageID = ...;
        ourMessage.ResponseTo = ...;
        ourMessage.Action = ...;
        // more headers set ...
  
        return headers;
      }
    }
  }
  </code></pre>
  
  <p>Every way we tried, we managed to get the WS-Addressing headers into the actual response, but could never get the WS-Security header in (actually we were just trying to put in a Security Header with TimestampID and Created/Expires elements). Every time we added the Security header in the Security header, the service just dropped the connection unexpectedly during serialization (after the SerializeReply call had completed).</p>
  
  <p>So here's my question. Does anyone know how to get the WS-Security headers into a WCF service with custom Formatter and custom Message implementation?</p>
  
  <p><strong>Update [26 Nov 2008]</strong>: We have an outstanding MS incident for this and the latest update we got from them was that WCF's current MessageVersion's don't seem to support those headers and need a custom binding implementation. The investigation continues for better approaches.</p>