MassTransit: Adding Headers to Publish Pipeline

2019-02-23 08:29发布

I'm using MassTransit 3.2.4 and I'm trying to add some header information for to my published messages but the code to set the header never seems to run. I'm not sure why this doesn't work.

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")
        ));
    });
});

On the consumer end I'm trying to read the header

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

Do I need to add an interceptor or something else in order to set header information?

1条回答
仙女界的扛把子
2楼-- · 2019-02-23 09:15

Figured it out after much guessing. Just had the ConfigurePublish in the wrong place

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");
    }));
}
查看更多
登录 后发表回答