此消息不支持该操作,因为它已被复制 [英] This message cannot support the operation because it has been copied
问题描述
我在WCF中遇到以下异常:此消息由于已被复制而无法支持该操作.
I get the following exception in WCF : This message cannot support the operation because it has been copied.
我正在使用CustomMessageInspector,这就是我处理传入消息的方式:
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;
}
根据MSDN页面,这是这样做的方法(CreateBufferedCopy),尽管我仍然收到异常.如果我注释掉此方法,一切正常吗?
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?
有什么想法吗?
推荐答案
这是因为消息的生存期仅持续使用一次.查看邮件内容或将内容复制到某处后,您将无法再次阅读该邮件.
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();
}
这篇关于此消息不支持该操作,因为它已被复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!