为什么NServiceBus OutgoingHeaders是静态的而不是ThreadStatic? [英] Why NServiceBus OutgoingHeaders is static and not ThreadStatic?

查看:85
本文介绍了为什么NServiceBus OutgoingHeaders是静态的而不是ThreadStatic?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当传出标头为静态时,NServiceBus如何保持一致性?

How is NServiceBus maintaining consistency when the outgoing headers is static?

这是否意味着如果我要为特定邮件设置传出标头,由于它是单例邮件,它将影响所有其他传出邮件吗?

Does it mean if I were to set the outgoing headers for a particular message, it will affect all other outgoing messages since it's a singleton?

namespace NServiceBus.MessageHeaders
{
  [ComVisible(false)]
  public class MessageHeaderManager : IMutateOutgoingTransportMessages
  {
    private static IDictionary<string, string> staticOutgoingHeaders = (IDictionary<string, string>) new Dictionary<string, string>();
    private IUnicastBus bus;
    [ThreadStatic]
    private static IDictionary<object, IDictionary<string, string>> messageHeaders;
   .
   .
   .
 }

但是,传入的标头似乎已正确标记为[ThreadStatic].

The incoming header seems to be correctly marked as [ThreadStatic] however.

解释.

================================================= ========

========================EDIT==============================

我想我试图理解,因为许多示例显示了以下代码:

I guess I'm trying to understand because many examples show the code below:

Bus.OutgoingHeaders["Test"] = g.ToString("N");

可追溯到:

IDictionary<string, string> IBus.OutgoingHeaders
{
  get
  {
    return ExtensionMethods.GetStaticOutgoingHeadersAction();
  }
}

设置为:

内部类Bootstrapper:INeedInitialization,IWantToRunWhenConfigurationIsComplete { 公共MessageHeaderManager管理器{放; }

internal class Bootstrapper : INeedInitialization, IWantToRunWhenConfigurationIsComplete { public MessageHeaderManager Manager { get; set; }

void INeedInitialization.Init()
{
  Configure.Instance.Configurer.ConfigureComponent<MessageHeaderManager>(DependencyLifecycle.SingleInstance);
}

public void Run()
{
  ExtensionMethods.GetHeaderAction = (Func<object, string, string>) ((msg, key) => this.Manager.GetHeader(msg, key));
  ExtensionMethods.SetHeaderAction = (Action<object, string, string>) ((msg, key, val) => this.Manager.SetHeader(msg, key, val));
  ExtensionMethods.GetStaticOutgoingHeadersAction = (Func<IDictionary<string, string>>) (() => this.Manager.GetStaticOutgoingHeaders());
}

}

当然,上面最后一行中的GetStaticOutgoingHeaders转到一个静态字段.

And of course the GetStaticOutgoingHeaders in the last line above goes to a static field.

我正在尝试为下一个消息设置标题.但是,如果我按照示例进行操作,最终将设置所有消息的标题.

I'm trying to figure out how to set the header, for the next message. But if I follow the examples, I end up setting the headers for ALL messages.

推荐答案

[由Udi更新] 如果要在要发送的特定邮件上设置标头,只需调用.SetHeader(key, value);消息对象上的方法.静态传出标头对于进程范围的数据很有用,例如登录用户在桌面应用程序中的用户. [结束更新]

[Update by Udi] If you want to set a header on a specific message that you're sending, just call the .SetHeader(key, value); method on the message object. The static outgoing headers is useful for process-wide data like who the logged-in user is in a desktop application.[End update]

MessageHeaderManagerIMutateOutgoingTransportMessages,这意味着它仅与邮件的发送有关.这里没有显示传入的邮件头.

MessageHeaderManager is a IMutateOutgoingTransportMessages which means it only is concerned with messages on their way out. There are no incoming message headers on display here.

messageHeaders与按消息设置的标头有关,例如发送时间或您可以从消息处理程序手动设置的任何内容.

messageHeaders is concerned with headers that are set per-message, like time sent, or anything you would set manually from a message handler.

staticOutgoingHeaders是在端点外缓存每个消息相同的所有标头的地方,因此不需要重新计算它们的值.这将包括诸如源计算机名称之类的内容.

staticOutgoingHeaders is a place to cache all headers that are the same for every single message out of an endpoint so that their values don't need to be recalculated. This would include things like the source machine name.

如果您查看该MutateOutgoing方法的内容,您会发现messageHeadersstaticOutgoingHeaders的所有键/值对都已添加到transportMessage.Headers集合中.此外,静态对象与Headers.Add(key, value)一起添加,而线程静态标头通过Headers[key] = value添加,以便线程静态集合中的项将覆盖同名的静态标头.

If you look into the guts of that MutateOutgoing method, you will see that all of the key/value pairs from both messageHeaders and staticOutgoingHeaders are added to the transportMessage.Headers collection. Additionally, the static ones are added with Headers.Add(key, value) while the thread-static headers are added via Headers[key] = value so that an item from the thread-static collection would override a static header of the same name.

这是一个链接到全文该类在GitHub上的来源,由V4.0.3的标记链接(在撰写本文时为最新),因此希望该链接不会过期.

Here's a link to the full source for this class on GitHub, linked by the tag for V4.0.3 (current at time of writing) so hopefully that link won't expire.

这篇关于为什么NServiceBus OutgoingHeaders是静态的而不是ThreadStatic?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆