Deserializing a Message


The one ubiquitous task in all receiving applications is Message deserialization. Message deserialization is another term for creating a Message from a serialized Message. Since we have already covered how to create a Message object, we have, for the most part, already covered parts of Message deserialization. More specifically, we have already covered how to create a Message from an underlying Stream or Byte via the XmlDictionaryReader type.

Remembering the discussion of the Message factory methods, one of the ways we can create the body of a Message is by passing an Object graph to a Message factory method. In a similar manner, we might need to deserialize an Object graph from an instance of a Message. To this end, the Message type defines members that deserialize the body of a Message object. The prototypes for these methods are shown here:

 public T GetBody<T>(); public T GetBody<T>(XmlObjectSerializer serializer);

The GetBody generic methods allow the caller to deserialize the contents of the body into an object of type T. One of the GetBody<T> methods accepts an XmlObjectSerializer, thereby providing an extensibility point for body deserialization. Regardless of which GetBody generic method we call, we must have specific knowledge of the type contained in the body of the Message. If the generic parameter used in these methods is not compatible with the body of the Message, a SerializationException is thrown.

Checking Whether a Message Is a SOAP Fault

As you have seen, an instance of the Message type can represent a SOAP message or a SOAP Fault. When a receiving application deserializes a Message, it must be able to determine whether the Message represents a SOAP Fault, because it is often the case that the execution path for a SOAP Fault is different from that of a SOAP message. To this end, the Message type defines the IsFault read-only property. In short, once an instance of a Message has been deserialized from an incoming Stream or Byte, the IsFault property indicates whether the Message represents a SOAP Fault and is typically one of the first checks the WCF infrastructure performs on a deserialized Message. We can illustrate the functionality of this property by changing the CreateAndShowMessage method from the preceding example, as shown here:

 private static void CreateAndShowMessage(MessageFault messageFault,                                          MessageVersion version) {   Message message = Message.CreateMessage(version,                                           messageFault,                                           "urn:SomeFaultAction");   // commented out for clarity   // Console.WriteLine("{0}\n", message.ToString());   // ** New code begins here **   MemoryStream stream = new MemoryStream();   // write the Message to a Stream   XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(       stream,null, null, false);   message.WriteMessage(writer);   writer.Flush();   stream.Position = 0;   // read the Message from the Stream   XmlDictionaryReader reader =       XmlDictionaryReader.CreateBinaryReader(stream, new         XmlDictionaryReaderQuotas());   message = Message.CreateMessage(reader, Int32.MaxValue, version);   // check if it is a Fault   Console.WriteLine("the message {0} a SOAP Fault",       message.IsFault ? "is" : "is not"); }

When this code executes (as part of the earlier code snippet), the following is generated:

 the message is a SOAP Fault the message is a SOAP Fault

Notice that the Message.IsFault property returns true for both of the Message objects created. It is important to note that this property returns true for all Message objects that represent a SOAP Fault, regardless of their encoding on the wire or the MessageVersion.




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

Similar book on Amazon
Learning WCF: A Hands-on Guide
Learning WCF: A Hands-on Guide
Essential Windows Communication Foundation (WCF): For .NET Framework 3.5
Essential Windows Communication Foundation (WCF): For .NET Framework 3.5
Programming WCF Services: Mastering WCF and the Azure AppFabric Service Bus
Programming WCF Services: Mastering WCF and the Azure AppFabric Service Bus
Microsoft  Windows  Communication Foundation Step by Step (Step By Step Developer Series)
Microsoft Windows Communication Foundation Step by Step (Step By Step Developer Series)

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