Wednesday, April 2, 2014

This message cannot support the operation because it has been copied

In order to implement few validations I have written separate method which expects message instance as parameter. In side that method I process the  input message . but once I try to run my service it throws above error and terminates the process Sad smile. after spending some time with the issue I could identify the issue.

according to the MSDN The body of a Message instance can only be consumed or written once. but I have tried several times to read message instance inside my validation logic.

from MSDN

“You can access the body of a Message only once, regardless of how it is accessed. A message object has a State property, which is initially set to Created. The three access methods described in the preceding list set the state to Written, Read, and Copied, respectively. Additionally, a Close method can set the state to Closed when the message body contents are no longer required. The message body can be accessed only in the Created state, and there is no way to go back to the Created state after the state has changed.”

There fore we need to use CreateMessageCopy method for reading the message.

   1: public bool Logic(Message message)
   2: {
   3:     MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue);
   4:  
   5:     var message1 = buffer.CreateMessage();
   7:  
   8:     buffer.Close();
   9:  
  10:     return true;
  11: }

Bingo its works Smile

No comments:

Post a Comment