1

使用 NServiceBus 4.0.11 我想打电话

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

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

所以实际上我想引用 Bus 的实例,以调用 OutgoingHeaders。尝试过 IWantCustomInitialization 但在其中调用 CreateBus 时给了我一个异常。INeedInitialization 也不是要走的路。

我应该如何调用 Bus.OutgoingHeaders["user"] = "john"; 在使用 NServiceBus.Host 时?

4

1 回答 1

1

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

除了在发送消息之前设置标头之外,您还可以通过message mutatorbehavior添加标头。

行为

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 。

于 2015-08-24T08:56:28.163 回答