WCF 中的 buffer.CreateMessage() 中的 2 个相同消息正确的方法是什么?

发布于 2024-12-28 23:42:47 字数 660 浏览 2 评论 0原文

从 WCF 中的 CreateBufferedCopy 创建 2 个相同副本的最佳方法是什么? 方法 1 或方法 2 为什么?

enter code here
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)

{
    MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
    request = buffer.CreateMessage();

    //approach 1  
    Message message1 = buffer.CreateMessage();
    Message message2 = buffer.CreateMessage();


    //approach 2  
    Message message1 = request;
    Message message2 = request;


    foreach (MessageHeader h in message1 .Headers)
    {
        Console.WriteLine("\n{0}\n", h);
    }
    return null;
}

What is best approach to create 2 identical copy from CreateBufferedCopy in WCF ?
approach 1 or approach 2 and why?

enter code here
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)

{
    MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
    request = buffer.CreateMessage();

    //approach 1  
    Message message1 = buffer.CreateMessage();
    Message message2 = buffer.CreateMessage();


    //approach 2  
    Message message1 = request;
    Message message2 = request;


    foreach (MessageHeader h in message1 .Headers)
    {
        Console.WriteLine("\n{0}\n", h);
    }
    return null;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2025-01-04 23:42:47

WCF 中的消息只能读取一次。这是因为它们可能是流式传输的,因此流式传输的数据不会神奇地重新

发送。要多次“处理”一条消息,您必须复制它,复制它的唯一方法是使用 MessageBuffer,就像方法 1 中所用的那样。处理消息可能只是检查正文以执行内容的数据相关路由,但一旦您要触摸正文,您就必须复制它,以便 WCF 基础结构的其余部分成功处理消息

请注意,如果全部你想做的是看看headers 你不需要复制消息,因为 headers 总是被缓冲的 - 它只是可能被流式传输的正文,

正如 @hyp 所说,方法 2 根本不复制消息 - 它只是为你提供了对同一消息的两个引用 -可能值得重新阅读有关引用类型和值类型的内容 - 这里有一篇文章可能会有所帮助

Messages in WCF are read-once. This is because they may be streamed and therefore the streamed data will not be magically resent

To "process" a message more than once you have to copy it and the only way to copy it is to use a MessageBuffer as you have in approach 1. Processing a message may be just inspecting the body to perform data dependent routing of the content but as soon as you are going to touch the body you must copy it for the message to be successfully processed by the rest of the WCF infrastructure

Note that if all you want to do is look at the headers you do not need to copy the message as headers are always buffered - it is only the body that is potentially streamed

as @hyp says, approach 2 does not copy the message at all - it just gives you two references to the same message - it may be worth rereading something about reference types and value types - here's an article that may help

樱花细雨 2025-01-04 23:42:47

我以前从未使用过 MessageBuffer,但方法 2 不会给你你想要的东西。在方法 2 中,您仅将请求的引用分配给两个对象,因此您仍然只有 1 个请求对象。换句话说,对 Message1 的更改将反映在 Message2 中。

I never used the MessageBuffer before but approach 2 won't give you what you're looking for. In approach 2 you're only assigning the reference of the request to two objects so you still have only 1 request object. In other words changes to Message1 will be reflected in Message2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文