title: WCF service client: Customized soap envelope not being sent on the wire
body: <p>I am calling a 3rd party web service from a WCF client. The problem is that the service requires a specific soap envelope format.</p>
  
  <p>The question <a href="https://stackoverflow.com/questions/11322104/wcf-soap-format">WCF Soap Format</a> deals with the same problem and the solution suggested was to write a CustomTextMessageEncoder. </p>
  
  <p>I have written a custom message encoder and done my custom formatting in the WriteMessage method taking the MSDN sample encoder as described in the topic <a href="http://msdn.microsoft.com/en-us/library/ms751486(v=vs.110).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms751486(v=vs.110).aspx</a>. </p>
  
  <p>The custom formatting removes the soap header content and adds some namespace prefixing required by the 3rd party service.</p>
  
  <p>I can see that the encoder is being called in the VS debugger and that it is producing the desired output:</p>
  
  <pre><code>        public override ArraySegment&lt;byte&gt; WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
      {
          // code from MSDN example
          MemoryStream stream = new MemoryStream();
          XmlWriter writer = XmlWriter.Create(stream, this.writerSettings);
          message.WriteMessage(writer);
          writer.Close();
  
          // My customizing code
          var targetStream = new MemoryStream();
          FormatStreamToMirthFormat(stream, targetStream);
  
          // code from MSDN example
          // put the contents of the stream into a byte array
          byte[] messageBytes = targetStream.GetBuffer();
          int messageLength = (int)targetStream.Position;
          stream.Close();
  
          int totalLength = messageLength + messageOffset;
          byte[] totalBytes = bufferManager.TakeBuffer(totalLength);
          Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength);
  
          ArraySegment&lt;byte&gt; byteArray = new ArraySegment&lt;byte&gt;(totalBytes, messageOffset, messageLength);
          return byteArray;
      }
  </code></pre>
  
  <p>I have configured service message tracing in the app.config to see what is actually being sent and the problem I have is that it appears that the incoming soap envelope is still being sent on the wire (the envelope that is being sent when using basicHttpBinding).</p>
  
  <p>Why is the incoming soap envelope and not my custom soap envelope being sent?</p>
  
  <p>The other thing I noticed is if I use the default binding "basicHttpBinding" then the message on the wire contains a Http header followed by the soap envelope:</p>
  
  <pre><code>&lt;HttpRequest xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace"&gt;
     &lt;Method&gt;POST&lt;/Method&gt;
     &lt;QueryString&gt;&lt;/QueryString&gt;
     &lt;WebHeaders&gt;
         &lt;VsDebuggerCausalityData&gt;uIDPo+hLJO6ZB9hNt7/+pVZglrYAAAAAus8ogaD9Y0O0ThkjxtbdzBlxO8Yn2yBJggkv3BRx/qsACQAA&lt;/VsDebuggerCausalityData&gt;
  &lt;/WebHeaders&gt;
  &lt;/HttpRequest&gt;
  &lt;s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope"&gt;..
  </code></pre>
  
  <p>whereas my message starts with the soap envelope. </p>
  
  <pre><code>&lt;s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope"&gt;
  </code></pre>
  
  <p>Thanks for any help.</p>