Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a buffered copy of WCF Message

Tags:

c#

.net

wcf

I have the following code in a message inspector to examine the response body. I understand the WCF Message can only be read once so I create a copy first. But with the following code I still get the error "This message cannot support the operation because it has been 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 don't feel comfortable using Int23.MaxValue there. What would be a reasonable size?

like image 730
Laguna Avatar asked Dec 28 '22 02:12

Laguna


1 Answers

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;
like image 119
Sixto Saez Avatar answered Dec 29 '22 14:12

Sixto Saez