无法从OperationContext向MassTransit消息添加标头吗?

编程入门 行业动态 更新时间:2024-10-25 22:36:11
本文介绍了无法从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.Current是null,因为它显然在与用户调用不同的线程上运行.

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应用程序的消息都具有此标头,并且希望在全局范围内进行设置. 推荐答案

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

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

更多推荐

无法从OperationContext向MassTransit消息添加标头吗?

本文发布于:2023-11-05 04:16:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:消息   OperationContext   MassTransit   标头吗

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!