3

I am making a Request from MassTransit state machine saga and wait for reply.

But there could be two errors coming back to me:

  • MyRequest.TimeoutExpired
  • MyRequest.Faulted

I don't care on which conditions the request was not fulfilled, I want both situations to result in an error message to be published.

However, I could not find any way to combine two outcomes with or condition, so I can have one handling case for both outcomes and not copy-paste my code.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83

1 Answers1

3

In this case, you should either create a custom activity (advanced, probably not necessary) or just create a method that is called from both When() conditions, so that you can reuse the behavior between statements.

Task PublishEvent(BehaviorContext<TInstance> context)
{
    var consumeContext = context.GetPayload<ConsumeContext>();

    return consumeContext.Publish(new MyEvent(...));
}

{
    During(MyRequest.Pending,
        When(MyRequest.Completed)
            .ThenAsync(PublishEvent),
        When(MyRequest.Faulted)
            .ThenAsync(PublishEvent));
}
Chris Patterson
  • 28,659
  • 3
  • 47
  • 59
  • Unfortunately, there is no `GetPayload` but only `TryGetPayload`, so I need to know the type of returned object... Works in the case described but not in other situations, when I want to schedule same message from different execution branches. – Alexey Zimarev Mar 29 '16 at 08:30
  • The type is ConsumeContext. – Chris Patterson Mar 29 '16 at 17:16