MassTransit:添加标题以发布管道 [英] MassTransit: Adding Headers to Publish Pipeline

查看:100
本文介绍了MassTransit:添加标题以发布管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MassTransit 3.2.4,并且尝试向发布的消息中添加一些标头信息,但是设置标头的代码似乎从未运行过.我不确定为什么这行不通.

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?

推荐答案

经过大量猜测后发现了问题.只是在错误的位置放置了ConfigurePublish

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

这篇关于MassTransit:添加标题以发布管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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