Can´t add header to MassTransit message from OperationContext?

∥☆過路亽.° 提交于 2019-12-08 10:00:32

问题


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

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

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

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

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.

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.

I know I can add the Header when publishing the message, but I wanted to have all messages originated from the web application to have this header, and was expecting have it set up globally.


回答1:


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.



来源:https://stackoverflow.com/questions/45945303/can%c2%b4t-add-header-to-masstransit-message-from-operationcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!