Create a buffered copy of a WCF message

I have the following code in the message inspector to check the response body. I understand that a WCF message can only be read once, so I create a copy first. But with the following code, I still get the error message "This message cannot support the operation because it was read." Am I missing something?

        MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue);
        Message copy = buffer.CreateMessage();
        message = copy;

        XmlDictionaryReader bodyReader = copy.GetReaderAtBodyContents();
        bodyReader.ReadStartElement("Binary");
        byte[] bodyBytes = bodyReader.ReadContentAsBase64();
        string messageBody = Encoding.UTF8.GetString(bodyBytes);

        return messageBody;

Also I do not feel comfortable using Int23.MaxValue. What would be a reasonable size?

+3
source share
1 answer

Try this code:

    MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue);
    message = buffer.CreateMessage();

    var copy = buffer.CreateMessage();
    XmlDictionaryReader bodyReader = copy.GetReaderAtBodyContents();
    bodyReader.ReadStartElement("Binary");
    byte[] bodyBytes = bodyReader.ReadContentAsBase64();
    string messageBody = Encoding.UTF8.GetString(bodyBytes);

    return messageBody;
+9
source

All Articles