.net - 此消息无法支持操作,因为它已被复制

标签 .net wcf

你好,

我在 WCF 中得到以下异常:This message cannot support the operation because it has been copyed.

我正在使用 CustomMessageInspector,这就是我处理传入消息的方式:

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),但我仍然遇到异常。如果我将此方法注释掉,一切正常吗?

有什么想法吗?

最佳答案

这是因为一条消息的生命周期只有一次使用。查看邮件内容或将内容复制到某处后,您将无法再次阅读该邮件。

private long DetermineAndLogMessageDiagnostics(Message message)
{
   buffer = message.CreateBufferedCopy(Int32.MaxValue);

   // Do something with the copied message

   reply = buffer.CreateMessage();
   buffer.Close();
}

关于.net - 此消息无法支持操作,因为它已被复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9518941/

相关文章:

c# - 使用 GUID 是生成随机字符串和数字的有效方法吗?

.net - 如何从 Entity Framework 中的多对多关系中获取数据?

c# - 系统.BadImageFormatException : 64bit MVC 2 project - with step by step instruction to reproduce the error

c# - WCF 中 BasicHttpBinding 和 NetHttpBinding 的区别

c# - 在 .NET (ffmpeg) 中启动时进程停止

c# - 如何使用 MongoDB 的官方 C# 驱动程序从 'document' 中删除一个 'ID'?

asp.net-mvc - 选择哪个:ASP.NET MVC或RESTful WCF?

c# - 如何使用 Windows 身份验证防止重复的 HTTP 请求

c# - WCF 点对点,那里有节点吗?

.net - 不同类型的托管 WCF 应用程序及其优势