title: How to deserialize WCF message using OperationContract
body: <p>I succeeded in building a WCF client generated by svcutil.exe from the WSDL. Using the generated client proxy class I can call the web service of an external service supplier. I also succeeded in coding a message inspector, as I need to log both raw XML request and response as full SOAP message to the database.</p>
  
  <p>For an emergency scenario I also need to be able to "import" a raw XML response. I found many hints on using <code>XMLSerializer</code> or deserializing WCF messages based on the message contract.</p>
  
  <p>But how can I deserialize a raw XML response based on an operation contract? For a first test I use one of the logged raw responses, save it to a file and now try to deserialize it to the response type as generated in the client proxy. Somehow I must succeed in calling <code>DeserializeReply()</code> from class <code>ClientOperation</code>. But how to get there? </p>
  
  <p>I happily accept any help as I'm quite new to WCF...
  TIA,
  Stefan</p>
  
  <p>This is what I tried after Marc's answer:</p>
  
  <pre><code>  public static RatingResult DeserializeResponseFromFile(string path)
    {
       var xmlReader = XmlReader.Create(path);
       var message = Message.CreateMessage(xmlReader, int.MaxValue, MessageVersion.Soap11);
       var readerAtBodyContents = message.GetReaderAtBodyContents();
       var dcs = new DataContractSerializer(typeof(RatingResult), "RatingResponse", "http://rating.webservice.xxx.de");
  
       // Error in line 6 position 7. 'EndElement' 'RatingResponse' from namespace
       // 'http://rating.webservice.xxx.de' is not expected.
       // Expecting element 'commonDataField'.
       var wsResult = (RatingResult)dcs.ReadObject(readerAtBodyContents);
  
       return wsResult;
    }
  </code></pre>
  
  <p></p>
  
  <p>This is part of the logged XML response file, that I'm trying to deserialize to type <code>RatingResponse</code>:</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="..." xmlns:soapenc="..." xmlns:xsd="..." xmlns:xsi="..."&gt;
    &lt;soapenv:Header soapenv:encodingStyle="..." /&gt;
    &lt;soapenv:Body soapenv:encodingStyle="..."&gt;
      &lt;p933:RatingResponse xmlns:p933="http://rating.webservice.xxx.de"&gt;
        &lt;RatingReturn href="#id0" /&gt;
      &lt;/p933:RatingResponse&gt;
      &lt;multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="..." xsi:type="p878:RatingResult" xmlns:p878="http://output.rating.webservice.xxx.de"&gt;
        &lt;commonData href="#id1" /&gt;
        &lt;acctData href="#id2" /&gt;
        &lt;resultData href="#id3" /&gt;
      &lt;/multiRef&gt;
      &lt;multiRef id="id1" soapenc:root="0" soapenv:encodingStyle="..." xsi:type="p719:RatingCommonData" xmlns:p719="http://input.rating.webservice.xxx.de"&gt;
        &lt;requestdate xsi:type="xsd:dateTime"&gt;2010-12-24T09:45:09.531Z&lt;/requestdate&gt;
        ...
  </code></pre>
  
  <p>I guess that the data contract serializer has problems deserializing the href's. Please note that the message I try to deserialize "by hand" was captured using my injected message inspector. In a "normal" call of the web service this message get deserialized without problems.</p>