Serializing a Message


Now that you’ve learned how to create a Message, let’s examine how to serialize all or part of a Message. For starters, all of the Message serialization methods on the Message type methods start with the word Write, and these methods accept parameters of type XmlWriter or XmlDictionaryWriter. The actual work of Message serialization is performed by the XmlWriter or XmlDictionaryWriter object, rather than directly by the Message object. Remembering the discussion of the XmlDictionaryWriter, this serialization is actually a two-step process of Message serialization and encoding. The available method prototypes for serializing a Message are listed here:

 public void WriteStartEnvelope(XmlDictionaryWriter writer); public void WriteStartBody(XmlDictionaryWriter writer); public void WriteStartBody(XmlWriter writer); public void WriteBody(XmlDictionaryWriter writer); public void WriteBody(XmlWriter writer); public void WriteBodyContents(XmlDictionaryWriter writer); public void WriteMessage(XmlDictionaryWriter writer); public void WriteMessage(XmlWriter writer);

The WriteMessage methods serialize the entire contents of the Message to the the Stream wrapped by the XmlWriter or XmlDictionaryWriter. Since these methods serialize the entire Message, they are more commonly used than any other Write method on the Message type.

The Message type also defines methods that allow more granular control over Message serialization. For example, the WriteBody methods serialize the body element tags and body element content to the Stream wrapped by the XmlWriter or XmlDictionaryWriter. The WriteBodyContents method, on the other hand, serializes the contents of the body element (and not the body tags) to the Stream wrapped by the XmlDictionaryWriter. The WriteStartEvelope method simply writes the <s:Envelope tag to the Stream wrapped by the XmlDictionaryWriter. Calling the WriteStartBody method immediately after calling WriteStartEnvelope writes the XML namespaces to the envelope and serializes the start of the body tag and completely omits the headers from the serialized content. In practice, if we need to exert control over Message serialization by using these methods, we will certainly want to serialize header block contents. This capability is indirectly available in the Message object model and is covered in the section “The Message Headers Type” later in this chapter. For now, keep in mind that if you want to serialize a Message manually, you must explicitly serialize the appropriate header blocks. There are no explicit methods for writing the end envelope or body tags. To write the end envelope and body tags, simply call the XmlWriter. WriteEndElement method as necessary.




Inside Windows Communication Foundation
Inside Windows Communication Foundation (Pro Developer)
ISBN: 0735623066
EAN: 2147483647
Year: 2007
Pages: 106
Authors: Justin Smith

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net