title: XmlSerialization for custom type in WCF service
body: <p>I'm newbie in WCF. I developed a WCF service and client for it. The service methods will retrieve custom data which uses custom XML serializer. I've read, in this case, contract methods should be marked with <code>[XmlSerializerFormat]</code>:</p>
  
  <pre><code>[ServiceContract]
  [XmlSerializerFormat]
  public interface ITSService
  {
      [OperationContract]
      [XmlSerializerFormat]
      ProtocolDocument GetReferenceData(string referenceType, SerializableDictionary&lt;string, string&gt; args);
  </code></pre>
  
  <p><code>ProtocolDocument</code> implements <code>IXmlSerializable</code>:</p>
  
  <pre><code>[XmlRoot("protocol", Namespace = Protocol30Namespace)]
  [Type(Name = "protocol", Namespace = Protocol30Namespace)]
  public class ProtocolDocument : ProtocolElement, ICloneable, IXmlSerializable
  </code></pre>
  
  <p>VS 2010 chooses <code>wsHttpBinding</code> by default. I don't need in security, so I turned it off. </p>
  
  <p>Here is the service configuration:</p>
  
  <pre><code>&lt;services&gt;
     &lt;service name="MyNamespace.TSService" 
              behaviorConfiguration="MyNamespace.TSServiceBehavior"&gt;
        &lt;host&gt;
           &lt;baseAddresses&gt;
              &lt;add baseAddress = "http://localhost:51944/TSService.svc" /&gt;
           &lt;/baseAddresses&gt;
        &lt;/host&gt;
        &lt;endpoint 
            address="" 
            binding="wsHttpBinding" 
            bindingConfiguration="nonSecurityWSHttpBinding"
            contract="MyNamespace.ITSService"&gt;
           &lt;identity&gt;
              &lt;dns value="localhost"/&gt;
           &lt;/identity&gt;
        &lt;/endpoint&gt;
        &lt;endpoint 
            address="mex" 
            binding="mexHttpBinding" 
            contract="IMetadataExchange"/&gt;
     &lt;/service&gt;
  &lt;/services&gt;
  &lt;behaviors&gt;
     &lt;serviceBehaviors&gt;
        &lt;behavior name="MyNamespace.TSServiceBehavior"&gt;
           &lt;serviceMetadata httpGetEnabled="true" /&gt;
           &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
        &lt;/behavior&gt;
     &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;bindings&gt;
     &lt;wsHttpBinding&gt;
        &lt;binding name="nonSecurityWSHttpBinding"&gt;
           &lt;security mode="None"&gt;
              &lt;transport clientCredentialType="None"/&gt;
           &lt;/security&gt;
        &lt;/binding&gt;
     &lt;/wsHttpBinding&gt;
  &lt;/bindings&gt;
  </code></pre>
  
  <p>Then I generated the client for this service, but the result could not be deserialized. Fiddler says SOAP wrapped serialized data into <code>GetReferenceDataResult</code> and <code>GetReferenceDataResponse</code>:</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://tempuri.org/ITourSystemService/GetReferenceDataResponse&lt;/a:Action&gt;
      &lt;a:RelatesTo&gt;urn:uuid:3d7f6dc0-4961-4bc5-b1fc-c9997af9fbd4&lt;/a:RelatesTo&gt;
    &lt;/s:Header&gt;
    &lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;GetReferenceDataResponse xmlns="http://tempuri.org/"&gt;
        &lt;GetReferenceDataResult&gt;
            &lt;header version="3.0" language="Russian"/&gt;
            &lt;references/&gt;
        &lt;/GetReferenceDataResult&gt;
      &lt;/GetReferenceDataResponse&gt;
    &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  </code></pre>
  
  <p>But the root element is missing! What should I do? </p>
  
  <p>PS Serialization impl:</p>
  
  <pre><code> void IXmlSerializable.ReadXml(XmlReader reader)
  {
      var serializer = new ProtocolDocumentXmlSerializer();
      serializer.Deserialize(this, reader);
  }
  
  void IXmlSerializable.WriteXml(XmlWriter writer)
  {
      //
      // Serialize everything except the root element, because it was already written by .NET XML-serialization mechanism
      var xmlSerializationFlags = XmlSerializationFlags.All &amp; ~XmlSerializationFlags.IncludeRootElement;
  
      var serializer = new ProtocolDocumentXmlSerializer();
      serializer.Serialize(this, writer, xmlSerializationFlags);
  }
  </code></pre>
  
  <p>Xml-serialization works well. It is already in use. I suppose smth wrong with my WCF-configuration.</p>