title: How to serialize WCF message using DataContractSerializer?
body: <p>I am trying to serialize WCF message using DataContractSerializer to get the message size (without using service trace viewer). Following is the code snippet:</p>
  
  <pre><code>public void BeforeSendReply(ref Message reply, object correlationState)
          {
              byte[] bytes = null;
              var messageBuffer = reply.CreateBufferedCopy(Int32.MaxValue);
              var message = messageBuffer.CreateMessage();
              var dcs = new DataContractSerializer(typeof(Message));
              using (var ms = new MemoryStream())
              {
                  dcs.WriteObject(ms, message);
                  bytes = ms.ToArray();
                  Console.WriteLine(String.Format("Message size = {0}", bytes.Count()));
              }
          }
  </code></pre>
  
  <p>On doing so it raises the following exception:</p>
  
  <blockquote>
    <p>Type
    'System.ServiceModel.Channels.BodyWriterMessage'
    cannot be serialized. Cons ider
    marking it with the
    DataContractAttribute attribute, and
    marking all of its  members you want
    serialized with the
    DataMemberAttribute attribute.  If the
    typ e is a collection, consider
    marking it with the
    CollectionDataContractAttribute.</p>
  </blockquote>
  
  <p>What can be done?</p>