title: WCF Request body is sometimes a stream and sometimes buffered?
body: <p>I have a WCF webservice. I am trying to do some logging whenever a request is received by implementing a MessageInspector and logging in the AfterReceiveRequest() event.</p>
  
  <p>For some reason whenever I send a request to the webservice using the WCFTestClient.exe everything works fine. The message is logged and the request proceeds as normal. </p>
  
  <p>But when I send a request to the webservice using SOAPUI as the client, making a copy of the request message causes the body to simply show <code>&lt;body&gt;... stream ...&lt;/body&gt;</code> and it fails to be loaded as an XML document later for the sake of validation.</p>
  
  <p>I'm guessing that a request from the WCFTestClient.exe is received with a buffered message body and a request from SOAPUI is received as a streamed body? How is this possible?</p>
  
  <ul>
  <li><p>Is there someway I can write some code that will safely make a copy of either version? I have yet to figure out how to safely copy a streamed version as CreateBufferedCopy() obviously does not achieve this.</p></li>
  <li><p>Or can I configure WCF to always create a buffered message body and never a stream?</p></li>
  </ul>
  
  <p>Here is the code I am using to log and copy the request message:</p>
  
  <pre><code>object IDispatchMessageInspector.AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
  {
      try
      {
  
          MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
          request = buffer.CreateMessage();
  
          Message copy = buffer.CreateMessage();
  
          LogRequest(copy);
  
          ValidateMessage(ref request);
      }
      catch (Exception e)
      {
          throw new FaultException&lt;&gt;()...
      }
      return null;
  }
  </code></pre>
  
  <p>The copy of the request message fails to be loaded into an XML document within the ValidateMessage() method if it came from SOAPUI with a streamed body. It succeeds to be loaded as an XML document if it comes from WCFTestClient.exe with a buffered body.</p>
  
  <pre><code>void validateMessage(ref System.ServiceModel.Channels.Message message)
  {
      XmlDocument bodyDoc = new XmlDocument();
      //This load throws exception if request came from SOAPUI with streamed body...
      bodyDoc.Load(message.GetReaderAtBodyContents());
      ...
  }
  </code></pre>
  
  <p>The exception thrown by the Load() method is:</p>
  
  <blockquote>
    <p>System.InvalidOperationException {"The specified node cannot be
    inserted as the valid child of this node, because the specified node
    is the wrong type."}</p>
    
    <p>at System.Xml.XmlDocument.AppendChildForLoad(XmlNode newChild,
    XmlDocument doc)    at
    System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)    at
    System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean
    preserveWhitespace)    at System.Xml.XmlDocument.Load(XmlReader
    reader)    at ...</p>
  </blockquote>