使用 NServiceBus.Host 调用 OutgoingHeaders

Call OutgoingHeaders using NServiceBus.Host

使用 NServiceBus 4.0.11 我想打电话给

Bus.OutgoingHeaders["user"] = "john";

Header Manipulation 示例展示了如何使用自定义主机调用它。 我想在使用 NServiceBus.Host.

时调用它

所以实际上我想引用 Bus 的实例,以调用 OutgoingHeaders。 尝试了 IWantCustomInitialization,但在其中调用 CreateBus 时出现异常。 INeedInitialization 也不是解决之道。

我该怎么称呼Bus.OutgoingHeaders["user"] = "john";使用 NServiceBus.Host?

阅读您的问题后,我认为您想将此 header 添加到要在 initialization/startup 期间或处理消息时发送的特定消息中。通常,header 具有更通用的行为,因为它们需要应用于多个消息。

除了在发送消息之前设置 header,您还可以通过 消息修改器 行为[添加 header =27=].

行为

public class OutgoingBehavior : IBehavior<SendPhysicalMessageContext>
{
    public void Invoke(SendPhysicalMessageContext context, Action next)
    {
        Dictionary<string, string> headers = context.MessageToSend.Headers;
        headers["MyCustomHeader"] = "My custom value";
        next();
    }
}

突变体

public class MutateOutgoingTransportMessages : IMutateOutgoingTransportMessages
{
    public void MutateOutgoing(object[] messages, TransportMessage transportMessage)
    {
        Dictionary<string, string> headers = transportMessage.Headers;
        headers["MyCustomHeader"] = "My custom value";
    }
}

文档

参见:http://docs.particular.net/nservicebus/messaging/message-headers#replying-to-a-saga-writing-outgoing-headers 示例。