MassTransit:将 Headers 添加到发布管道

MassTransit: Adding Headers to Publish Pipeline

我正在使用 MassTransit 3.2.4,我正在尝试为我发布的消息添加一些 header 信息,但是设置 header 的代码似乎从来没有 运行.我不确定为什么这不起作用。

var bus = Bus.Factory.CreateUsingRabbitMq(config =>
{
    var host = config.Host(new Uri("rabbitmq://localhost/"), h {});
    config.ReceiveEndpoint(host, "TestPublisher", e => 
    { 
        e.ConfigurePublish(x => x.UseSendExecute(context =>
            context.Headers.Set("HeaderKey", "HeaderValue")
        ));
    });
});

在消费者端,我正在尝试阅读 header

public Task Consume(ConsumeContext<IActionHappened> context)
{
    var headerValue = context.Headers.Get("HeaderKey", "Default Value");
}

是否需要添加拦截器或其他东西才能设置header信息?

猜了好久才明白。只是将 ConfigurePublish 放错了地方

var bus = Bus.Factory.CreateUsingRabbitMq(config => 
{
    var host = config.Host(new Uri("rabbitmq://localhost/"), h => {});
    config.ConfigurePublish(x => x.UseSendExecute(context => 
    {
        context.Headers.Set("HeaderKey", "HeaderValue");
    }));
}