masstransitautomatonymous

Abort chain of activities for MassTransit Saga


I'm trying to figure out how to abort a chain of activities, if one activity decides that there is something wrong, and the Saga should be Finalized.

For example:

Initially(
    When(UpdateRequested)
        .Activity(x => x.OfType<InitialSetup>())
        .Activity(x => x.OfType<StartUpdating>())
        .TransitionTo(Updating)

InitialSetup will configure the Saga based on some information loaded from the database. Let's that that it realizes that the data needed is missing, and the Saga should be finalized, before StartUpdating activity is run.

How can I achieve that?


Solution

  • You can throw an exception, catch it, and use the catch branch to finalize the saga.

    Initially(
        When(UpdateRequested)
            .Activity(x => x.OfType<InitialSetup>()) // activity throws
            .Activity(x => x.OfType<StartUpdating>())
            .TransitionTo(Updating)
            .Catch<MissingDataException>(x => x.Finalize())
    );