Search code examples
c#eventsrabbitmqmasstransitsaga

There is any way to access headers of published event in masstransit saga?


Is there any way to access headers in saga when I published event like this ?

 await busControl.Publish<IOrderCreated>(new
        {
            OrderId = dto.Id
        }, context =>
        {
            context.Headers.Set(LogConstansts.Common.OperationId,Guid.Parse(values.Single()).ToString());
            context.Headers.Set(LogConstansts.QueueMessageHeaderNames.Publisher, Request.RequestUri.AbsoluteUri);
        });

and then in saga

Initially(
           When(OrderCreated)
            .Then(context =>
                  {
                    //get headers somehow?
                     context.Instance.OrderId = context.Data.OrderId; 
                  })

Solution

  • Yes, you can access it by fetching the consume context payload from the behaviour context:

    Initially(
        When(OrderCreated)
            .Then(context =>
            {
                ConsumeContext<IOrderCreated> c;
                if (context.TryGetPayload(out c))
                {
                    c.Headers.Get<string>("myheader");
                    // do something
                }
                context.Instance.OrderId = context.Data.OrderId; 
            })