How does the WCF Message.ToString () method work?

All methods on System.ServiceModel.Channels.Message allow you to read the message body only once and fail if it is called after the message has been read. The msdn documentation confirms that only the body of the message can be read. However, if you call ToString() on an already read message, you seem to get the entire envelope for soap, body and all.

Thus, in my case, it would be possible to access the body even after it has been read, if only its methods allow it.

Is there something I am missing here? Does ToString() as a workaround so that the body is not reliable in some situations?

In my case, I work on some error logs for some WCF operations and get the original message from OperationContext.RequestContext.RequestMessage . I am logging the message with ToString() , because this is the only way to find so that I can log the body of the message.

+4
source share
3 answers

ToString can print the body of a message, but this is not guaranteed. There are many types of Message objects (this is an abstract class). Some of them buffer the entire body, while others only have a direct access reader. Implementations of messages that buffer the message can write the body when calling ToString and what you see. But this is not guaranteed for all types of messages. In many cases, the body is simply written as "... thread ..." when ToString is called.

+8
source

I just wanted to share the fact that I successfully used the message.toString method to save the base64 lowercase encoding obtained from byte[] , which is lost when creating the message buffer (the buffer is the only way to make a copy of the original message, so it was necessary in my code) . Having created the buffer, using it to create XML and edit XML, I restored the base64 string using the string saved from the toString method. I would not recommend doing it this way, but in this case it was the only option, and I know that it will work in my specific code. However, I usually think that this should be the last option.

+2
source

You must create a copy of the message, then read . For instance:

 using (MessageBuffer messageBuffer = message.CreateBufferedCopy(Int32.MaxValue)) { Message restoredMessage = messageBuffer.CreateMessage(); message = messageBuffer.CreateMessage(); return MessageToString(ref restoredMessage); } 

RequestContext.RequestMessage is missing in a one-way call, so ToString will not work, otherwise RequestMessage.ToString () will return the contents of the message in the line

0
source

All Articles