Can´t add header to MassTransit message from OperationContext?

1.8k views Asked by At

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

There are 1 answers

0
Jenser Almeida On

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.