tags:

views:

95

answers:

1

Hi,

I have a WCF error handler that uses the IErrorHandler interface. In the HandleError method of this error handler I write the error exception to the log. This all works fine. However, I have a requirement to also write the actual message that raised the exception to the log as well and this is not something passed into the HandleError method.

So, I have been looking at OperationContext to see if that helps by doing something like the following:

MessageBuffer buffer = OperationContext.Current.RequestContext.RequestMessage.CreateBufferedCopy(int.MaxValue);

Message message = buffer.CreateMessage();
using (XmlDictionaryReader reader = message.GetReaderAtBodyContents())
{
   string content = reader.ReadContentAsString();
}

However, I keep getting "This message cannot support the operation because it has been copied" as I assume the message has been copied previously somewhere by the framework? Anyway, I am now at a loss as to how to achieve the functionality required. Does anyone have any suggestions?

Thanks in advance

+1  A: 

OperationContext.Current.RequestContext.RequestMessage points to the actual message so you can just use that.

Maurice
But I need to access the message body and I get errors whenever I try to do this hence why I thought you had to try and create a buffered copy, but that returns an error as well?Thanks
Jon Archway
Ah, never mind, I am being a dope! I can just use it as it is without having to get the body or anything. Doh!
Jon Archway
A buffered copy of something you use when you want to send the same message multiple times. You have to create it before the message is send in the first place. In this case you have the complete message as a string using the ToString() method.
Maurice