无法从OperationContext向MassTransit消息添加标头吗? [英] Can´t add header to MassTransit message from OperationContext?

查看:123
本文介绍了无法从OperationContext向MassTransit消息添加标头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将标题添加到Masstransit消息中,该消息包含来自WCF Web应用程序上当前登录的用户的信息.

I am trying to add a header to a Masstransit message containing information from the currently logged user on a WCF Web application.

因此在我的Web应用程序中,当应用程序启动时,我有以下内容:

So at my web app, I have the following when the app starts:

IBusControl bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
    cfg.Host(new Uri("rabbitmq://localhost/test"), r =>
    {
        //...
    });

    cfg.ConfigureSend(
        s => s.UseSendExecute(
            c => c.Headers.Set(Constants.UserInfo, 
           OperationContext.Current.Channel.Extensions.Find<PropertyUserInfo>().Info)));
});

我使用IoC容器在应用程序(单一作用域)中仅创建一次IBusControl,然后将IBusControl注入到Web服务中.

I am using an IoC container to create IBusControl only once in the application (singleton scope), then IBusControl gets injected into the Web service.

请注意,当我创建服务总线时,OperationContext.Current不存在,我希望在请求上下文中调用lambda c= > c.Headers.Set(...)

Note that OperationContext.Current does not exists when I am creating the servicebus, I am expecting the lambda c= > c.Headers.Set(...) to be called within the request context

现在,当用户发出请求时,我正在使用请求-响应模式(尽管我认为这并不重要)

Now, when the user makes a request, I am using a request-response pattern (although I dont think this matters)

var requestClient = _bus.CreateRequestClient<AddTicketRequest, AddTicketResponse>(uri, timeout);
var response = requestClient.Request(requestMessage);

问题是,当执行此代码时,Masstransit尝试将标头添加到消息中,OperationContext.Currentnull,因为它显然在与用户调用不同的线程上运行.

The problem is, when this code is executed, and Masstransit tries to add the header to the message, OperationContext.Current is null as apparently it is running on a different thread then the user call.

奇怪的是,最终Masstransit开始从正确的线程调用UseSendExecute,一切开始正常工作.而且我必须重新启动IIS才能再次复制该错误(!?).

Oddly enough, eventually Masstransit starts to call UseSendExecute from the right thread, and everything starts to work. And I have to restart IIS to replicate the bug again (!?).

有人遇到过这个问题吗?谢谢.

Has anybody ever had this problem ? thanks.

我知道我可以在发布消息时添加标头,但是我希望所有来自Web应用程序的消息都具有此标头,并且希望在全局范围内进行设置.

推荐答案

问题 https://github.com/MassTransit/MassTransit/issues/921 看起来很相关.我将采用相同的解决方法,并将使用IBusControl的包装器.

The issue https://github.com/MassTransit/MassTransit/issues/921 looks related. I'll go with the same workaround, and I will work with a wrapper around IBusControl.

这篇关于无法从OperationContext向MassTransit消息添加标头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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