简体   繁体   中英

WCF : This message cannot support the operation because it has been copied

private static string ReadDefaultMessageBody(ref Message message)
{
    const string XmlReaderName = "binary";

    if (message.IsEmpty)
    {
        return string.Empty;
    }

    MessageBuffer buffer = message.CreateBufferedCopy(int.MaxValue);
    try
    {
        // Copy the original message and use it for reading.
        Message messageCopy = buffer.CreateMessage();

        // Re-create original message  
        message = buffer.CreateMessage();

        // Dump payload from original message  
        // It is in either plain text or in base64 encoded string  
        using (var reader = messageCopy.GetReaderAtBodyContents())
        {
            return string.Compare(reader.Name, XmlReaderName, StringComparison.OrdinalIgnoreCase) == 0
                            ? Encoding.Default.GetString(Convert.FromBase64String(reader.ReadInnerXml()))
                            : reader.ReadOuterXml();
        }                
    }
    finally
    {
        buffer.Close();
    }
}

I referred some links on stackoverflow : This message cannot support the operation because it has been copied

and

MessageInspector message: "This message cannot support the operation because it has been copied."

From the first link it seems like that the message cannot be copied more than once. And from the second link it seems like the message can be copied more than once if we recreate the message.

Can someone point to the mistake that i am doing. Since this method is called more than once and i get the following error : "This message cannot support the operation because it has been copied"

Exception: System.InvalidOperationException: This message cannot support the operation because it has been copied.\\r\\n at System.ServiceModel.Channels.Message.CreateBufferedCopy(Int32 maxBufferSize)

Try this code:

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

    var copy = buffer.CreateMessage();

As explained on the original answer

The message variable is passed into your code by reference and contains the message WCF will work with. It cannot be in a "already read" state to be valid for WCF use. You can, however, call buffer.CreateMessage() multiple times to create clones of the actual message WCF is working with. If you want to inject something into the existing message, you can create a new message from the old message and pass the modified message back to WCF

Try this,It works for me

      var bufferedMessage = message.CreateBufferedCopy(int.MaxValue);

      using (var messageToRead = bufferedMessage.CreateMessage())
      {
        //Code Block
        messageToRead.Close();       
      }
      
      message = bufferedMessage.CreateMessage();   

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