Banshee
Banshee

Reputation: 15817

This message cannot support the operation because it has been copied

Hi,

I get the following exception in WCF : This message cannot support the operation because it has been copied.

I am using CustomMessageInspector and this is how Im handling the incoming message :

private long DetermineAndLogMessageDiagnostics(Message message)
{
    MessageBuffer buffer;
    Message bufferMessage;

    if (!message.IsFault && !message.IsEmpty)
    {
        buffer = message.CreateBufferedCopy(Int32.MaxValue);
        bufferMessage = buffer.CreateMessage();

        var messageBodyReader = bufferMessage.GetReaderAtBodyContents();
        var messageBody = messageBodyReader.ReadOuterXml();

        double bodySizeInBytes = Encoding.UTF8.GetByteCount(messageBody);

        return long.Parse(Math.Ceiling(bodySizeInBytes / 1024).ToString());
    }
    return 0;  
} 

According to MSDN pages this is the way to do it (CreateBufferedCopy) byt I still get the exception. If I comment this method out everything works fine?

Any idea?

Upvotes: 3

Views: 5491

Answers (1)

vulkanino
vulkanino

Reputation: 9134

It is because the lifetime of a message only lasts for one use. Once you've looked at the contents of a message, or copied the contents somewhere, you can't read the message again.

private long DetermineAndLogMessageDiagnostics(Message message)
{
   buffer = message.CreateBufferedCopy(Int32.MaxValue);

   // Do something with the copied message

   reply = buffer.CreateMessage();
   buffer.Close();
}

Upvotes: 4

Related Questions