简体   繁体   中英

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM