Copying Messages


In some cases it might be necessary to create a buffered copy of an existing message. The Message type contains the following instance method for this purpose:

 public MessageBuffer CreateBufferedCopy(Int32 maxBufferSize) { ... }

Creating a copy of a Message is fairly straightforward, but it does cause a state change within the Message being copied. If not properly used, this state change can cause problems when working with the Message object that was just copied. When the CreateBufferedCopy method is invoked, the state property of the calling instance must be MessageState.Created. If the state property is set to any other value, the method will throw an InvalidOperationException. By the time the call to CreateBufferedCopy returns, the state of the calling instance has changed to MessageState.Copied. If the method call succeeds, an instance of a System.ServiceModel. Channels.MessageBuffer is returned. MessageBuffer defines a CreateMessage instance method that returns a Message. The newly created Message has a state of Message.Created. The following code snippet demonstrates how to copy a message:

 Message msg = Message.CreateMessage(MessageVersion.Default,                                     "urn:SomeAction",                                     "Something in the body"); Console.WriteLine("Starting Message state: {0}\n", msg.State); Console.WriteLine("Message:\n{0}\n", msg.ToString()); MessageBuffer buffer = msg.CreateBufferedCopy(Int32.MaxValue); Console.WriteLine("Message state after copy: {0}\n", msg.State); Message msgNew = buffer.CreateMessage(); Console.WriteLine("New Message State: {0}\n",msgNew.State); Console.WriteLine("New Message:\n{0}\n", msgNew.ToString());

When this code executes, the following output is generated:

 Starting Message state: Created Message: <s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing"   xmlns:s="http://www.w3.org/2003/05/soap-envelope">   <s:Header>     <a:Action s:mustUnderstand="1">urn:SomeAction</a:Action>   </s:Header>   <s:Body>     <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">       Something in the body</string>   </s:Body> </s:Envelope> Message state after copy: Copied New Message State: Created New Message: <s:Envelope xmlns:a=http://www.w3.org/2005/08/addressing   xmlns:s="http://www.w3.org/2003/05/soap-envelope">   <s:Header>     <a:Action s:mustUnderstand="1">urn:SomeAction</a:Action>   </s:Header>   <s:Body>     <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">       Something in the body     </string>   </s:Body> </s:Envelope>

Notice the state of the Message right after the CreateBufferedCopy method call and the state of the new Message. The CreateBufferedCopy method has limited uses, and the culprit is the state changes within the copied Message. It does prove useful, however, in the case of multiparty messaging scenarios, like the one in PeerChannel. In PeerChannel, one Message object must be copied several times, and the copies are sent to the various neighbors in the mesh.




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