title: WCF Soap Message Remove 'To' Header
body: <p>Although there are other questions on this subject, none really cover this exact problem. I rarely ask questions because I can usually find the answer. </p>
  
  <p>We are using a WCF client to talk to a Java web service that uses Soap 1.1. I had to create a custom WseHttpBinding to add a userNameToken. The problem is that when the request is serialized, a bunch of header elements are added that the Java service hates. These are specifically:</p>
  
  <p>Action
  MessageID
  ReplyTo
  To</p>
  
  <p>The actual header looks like this:<br></p>
  
  <pre><code> &lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
   &lt;s:Header&gt;
   &lt;a:Action s:mustUnderstand="1"/&gt;
   &lt;a:MessageID&gt;urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&lt;/a:MessageID&gt;
   &lt;a:ReplyTo&gt;&lt;a:Address&gt;http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous&lt;/a:Address&gt;&lt;/a:ReplyTo&gt;
   &lt;a:To s:mustUnderstand="1"&gt;https://xxx.xxx.xxx&lt;/a:To&gt;
  
   &lt;MORE SECURITY ELEMENTS...&gt;
   &lt;/s:Header&gt;
  </code></pre>
  
  <p>After researching my options, I couldn't find a way to remove those without writing a custom encoder or manipulating the message before it was sent. I chose the latter.</p>
  
  <p>Using a custom behavior, I used the IClientMessageInspector to implement a messageInspector class. In the BeforeSendRequest method, I used the following:</p>
  
  <pre><code>message.Headers.RemoveAll("Action", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
  message.Headers.RemoveAll("MessageID","http://schemas.xmlsoap.org/ws/2004/08/addressing");
  message.Headers.RemoveAll("ReplyTo", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
  message.Headers.RemoveAll("To", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
  </code></pre>
  
  <p>I was quite surprised that it removed all of the headers exception for one. Here is the result:</p>
  
  <pre><code>&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
  &lt;s:Header&gt;
  &lt;a:To s:mustUnderstand="1"&gt;https://pilot.eidverifier.com/uru/soap/ut/usv3&lt;/a:To&gt;
  &lt;MORE SECURITY ELEMENTS...&gt;
  &lt;/s:Header&gt;
  </code></pre>
  
  <p>As you can see, the 'To' element is still there. So, my question is why? Does it require that to send the message? </p>
  
  <p>I can't help but feel that I'm approaching this the wrong way. Is there a way to make mustUnderstand false? Is there a property setting that removes all of the extra elements? I'm in release mode.</p>
  
  <p>Any help or direction is appreciated.</p>