本文介绍了将值添加到MassTransit.RabbitMq中的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是MassTransit 3.0.0.0,我很难理解如何在请求-响应方案中拦截消息,并将消息添加到我可以在接收者端读取的标头字段中.

I am using MassTransit 3.0.0.0 and I have a hard time understanding how to intercept messages in a Request-Response scenario on their way out and add some information to the headers field that I can read on the receiver's end.

我正在查看中间件, MassTransit文档-请参阅观察者警告-但在发送"中获得的上下文是只是无法访问Headers字段的Pipe上下文,因此我无法更改它.我使用了中间件页面中提供的示例.

I was looking at the Middleware, as recommended in the MassTransit docs - see Observers warning - but the context you get on the Send is just a Pipe context that doesn't have access to the Headers field so I cannot alter it. I used the sample provided in Middleware page.

然后我看了看IPublishInterceptor

I then, looked at IPublishInterceptor

public class X<T> : IPublishInterceptor<T> where T : class, PipeContext
{
    public Task PostPublish(PublishContext<T> context)
    {
        return new Task(() => { });
    }

    public Task PostSend(PublishContext<T> context, SendContext<T> sendContext)
    {
        return new Task(() => { });
    }

    public Task PrePublish(PublishContext<T> context)
    {
        context.Headers.Set("ID", Guid.NewGuid().ToString());
        return new Task(() => { });
    }

    public Task PreSend(PublishContext<T> context, SendContext<T> sendContext)
    {
        context.Headers.Set("ID", Guid.NewGuid().ToString());
        return new Task(() => { });
    }
}

这是非常清楚和简洁的.但是,我不知道在哪里使用它以及如何将其链接到其他基础架构.就目前而言,这只是一个没有真正链接到任何东西的接口.

Which is very clear and concise. However, I don't know where it is used and how to link it to the rest of the infrastructure. As it stands, this is just an interface that is not really linked to anything.

推荐答案

如果在发送消息时需要添加标头,则可以将中间件组件添加到发送"或发布"管道中,如下所示.请注意,发送"过滤器将应用于所有邮件,而发布"过滤器将仅应用于已发布的邮件.

If you need to add headers when a message is being sent, you can add middleware components to either the Send or the Publish pipeline as shown below. Note that Send filters will apply to all messages, whereas Publish filters will only apply to messages which are published.

// execute a synchronous delegate on send
cfg.ConfigureSend(x => x.Execute(context => {}));

// execute a synchronous delegate on publish
cfg.ConfigurePublish(x => x.Execute(context => {}));

可以在总线或单个接收端点上配置中间件,并且这些配置在其配置位置是本地的.

The middleware can be configured on either the bus or individual receive endpoints, and those configurations are local to where it's configured.

这篇关于将值添加到MassTransit.RabbitMq中的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 05:48