在 Masstransit saga 中捕获异常不起作用

Doesn't work exception catching in Masstransit saga's

我尝试将 saga 与 MassTransit 7.2.2 一起使用。 我的传奇中有下一个事件:

During(Submitted,
     When(OrderAccepted)
     .Then(x =>
     {
         logger.LogInformation($"Order {x.Instance.OrderId} accepted");
         throw new Exception("TEST");
     })
     .Catch<Exception>(x =>
     {
         x.If(
             context => context.Data.OrderId == 1002,
             activityBinder =>
                 activityBinder
                     .Then(y =>
                     {
                         logger.LogInformation($"Order {y.Instance.OrderId} catch exception and pass to Rejected");
                     })
                     .TransitionTo(Rejected)
         );

         return x;
     })
     .ThenAsync(c =>
     {
         return TakeProductCommand(c);
     })
     .TransitionTo(Accepted));

在上面的代码中,我想捕获在处理当前事件期间可能引发的任何异常,并将 saga 置于 Rejected 状态。但它并不像我想的那样工作。我根本没有进入 Catch 处理程序。

我做错了什么?

您的代码有点格式错误,并且破坏了构建器模式链。

The changes are subtle, but significant.

During(Submitted,
     When(OrderAccepted)
     .Then(x =>
     {
         logger.LogInformation($"Order {x.Instance.OrderId} accepted");
         throw new Exception("TEST");
     })
     .Catch<Exception>(x =>
         x.If(
             context => context.Data.OrderId == 1002,
             activityBinder =>
                 activityBinder
                     .Then(y =>
                     {
                         logger.LogInformation($"Order {y.Instance.OrderId} catch exception and pass to Rejected");
                     })
                     .TransitionTo(Rejected)
         )
     )
     .ThenAsync(c =>
     {
         return TakeProductCommand(c);
     })
     .TransitionTo(Accepted));