You are developing a Windows Communication Foundation (WCF) service.
You write a method named Submit that accepts messages of the type System.ServiceModel.Channels.Message.
You need to process the body of the incoming messages multiple times in the method.
Which two actions should you perform? (Each correct answer presents part of the solution. Choose two.)
A.
Use the GetBody method of the Message class to read the content of the messages.
B.
Use the CreateBufferedCopy method of the Message class to load the messages into memory.
C.
Use the WriteBodyContents method of the BodyWriter class to make a copy of the messages.
D.
Use the CreateMessage method of the MessageBuffer class to make a copy of the messages.
Explanation:
The body of a Message instance can only be accessed or written once. If you want to access a Message instance more than once,
you should use the MessageBuffer class to completely store an entire Message instance into memory.
A MessageBuffer instance is constructed by calling CreateBufferedCopy of a Message instance.Calling MessageBuffer.CreateMessage() method creates an identical copy of the original Message instance you previously provided to
the CreateBufferedCopy method of a Message instance. You can then save the message to a durable storage.Message.CreateBufferedCopy Method
(http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.message.createbufferedcopy(v=vs.95).aspx)MessageBuffer.CreateMessage Method
(http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.messagebuffer.createmessage.aspx)Example:
private byte[] ConvertMessageToByteArray(ref Message message)
{
MemoryStream stream = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = System.Text.Encoding.UTF8;
XmlWriter writer = XmlWriter.Create(stream, settings);MessageBuffer buffer = message.CreateBufferedCopy(int.MaxValue);
message = buffer.CreateMessage();message.WriteMessage(writer);
message = buffer.CreateMessage();
writer.Flush();
stream.Flush();
byte[] retval = stream.ToArray();
return retval;
}
BD