title: Adding Custom WCF header to Endpoint Programatically for Reliable Sessions
body: <p>I'm building a WCF router and my client uses Reliable Sessions. In this scenario when the client opens a channel a message is sent (establishing a Reliable Session?). Its contents is as follows:</p>
  
  <pre><code>&lt;s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"&gt;
    &lt;s:Header&gt;
      &lt;a:Action s:mustUnderstand="1"&gt;http://docs.oasis-open.org/ws-rx/wsrm/200702/CreateSequence&lt;/a:Action&gt;
      &lt;a:MessageID&gt;urn:uuid:1758f794-c5d3-4573-b252-7a07344cc257&lt;/a:MessageID&gt;
      &lt;a:To s:mustUnderstand="1"&gt;http://localhost:8010/RouterService&lt;/a:To&gt;
    &lt;/s:Header&gt;
    &lt;s:Body&gt;
      &lt;CreateSequence xmlns="http://docs.oasis-open.org/ws-rx/wsrm/200702"&gt;
        &lt;AcksTo&gt;
          &lt;a:Address&gt;http://www.w3.org/2005/08/addressing/anonymous&lt;/a:Address&gt;
        &lt;/AcksTo&gt;
        &lt;Offer&gt;
          &lt;Identifier&gt;urn:uuid:64a12658-71d9-4967-88ec-9bb0610f7ecb&lt;/Identifier&gt;
          &lt;Endpoint&gt;
            &lt;a:Address&gt;http://www.w3.org/2005/08/addressing/anonymous&lt;/a:Address&gt;
          &lt;/Endpoint&gt;
          &lt;IncompleteSequenceBehavior&gt;DiscardFollowingFirstGap&lt;/IncompleteSequenceBehavior&gt;
        &lt;/Offer&gt;
      &lt;/CreateSequence&gt;
    &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  </code></pre>
  
  <p>The problem here is that the headers do not contain any information I can use to look up what service to route the message to. In Busatmante's router sample code she gets around this by adding a header to the endpoint:</p>
  
  <pre><code>  &lt;client&gt;
      &lt;endpoint address="http://localhost:8010/RouterService" binding="ws2007HttpBinding"
        bindingConfiguration="wsHttp"
        contract="localhost.IMessageManagerService" &gt;
        &lt;headers&gt;
          &lt;Route xmlns="http://www.thatindigogirl.com/samples/2008/01" &gt;http://www.thatindigogirl.com/samples/2008/01/IMessageManagerService&lt;/Route&gt;
        &lt;/headers&gt;
      &lt;/endpoint&gt;
    &lt;/client&gt;
  </code></pre>
  
  <p>When the reliable session is opened the message contains this custom header. </p>
  
  <pre><code>&lt;Route a:IsReferenceParameter="true" xmlns="http://www.thatindigogirl.com/samples/2008/01"&gt;http://www.thatindigogirl.com/samples/2008/01/IMessageManagerService&lt;/Route&gt;
  </code></pre>
  
  <p>This is great; however, I have a requirement to configure the client programatically. I figured that the ChannelFactory Endpoint would have a Header object to which I could manually add my custom header. Unfortunately it does not. So I did some searching and found some recomendations to extend WCF by implementing an IClientMessageInspector to add my header and adding it as a behavior to my endpoint. </p>
  
  <pre><code>public class ContractNameMessageInspector : IClientMessageInspector {
  
      private const string HEADER_NAME = "ContractName";
      private readonly string _ContractName;
  
      public ContractNameMessageInspector(string contractName) {
          _ContractName = contractName;
      }
  
      #region IClientMessageInspector Members
  
      public void AfterReceiveReply(ref Message reply, object correlationState) { }
  
      public object BeforeSendRequest(ref Message request, IClientChannel channel) {
  
          HttpRequestMessageProperty httpRequestMessage;
          object httpRequestMessageObject;
  
          if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject)) {
              httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
              if (httpRequestMessage != null &amp;&amp; string.IsNullOrEmpty(httpRequestMessage.Headers[HEADER_NAME])) {
                  httpRequestMessage.Headers[HEADER_NAME] = this._ContractName;
              }
          }
          else {
              httpRequestMessage = new HttpRequestMessageProperty();
              httpRequestMessage.Headers.Add(HEADER_NAME, this._ContractName);
              request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
          }
          return null;
      }
  
      #endregion
  }
  </code></pre>
  
  <p>So when my client makes a service call the message contains the custom header but the message establishing the Reliable Sessions still does not. </p>
  
  <p>So my question is; how do I add a custom header to the Endpoint programatically in such a way that the reliable session message contains it?</p>
  
  <p>Many Thanks</p>