有没有办法在masstransit saga中访问已发布事件的标题?

时间:2017-08-09 19:32:42

标签: c# events rabbitmq masstransit saga

当我发布这样的事件时,有没有办法在saga中访问标题?

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

然后在传奇

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

1 个答案:

答案 0 :(得分:3)

是的,您可以通过从行为上下文中获取使用上下文有效内容来访问它:

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