My activities throw exceptions from time to time during the execution, so I've implemented the Faulted methods of Activity<TInstance>
to handle that, discarding the changes made in the Execute
method. I thought that there's some wiring underneath in Automatonymous that makes it so that the Faulted method executes when the Execute
method throws an exception and then calls the Faulted methods for the activities that were executed already. It turns out that there's no such thing, as my Faulted methods are never executed.
Should I call those myself in a try/catch block instead? I could produce the BehaviorExceptionContextProxy
based on BehaviorContext
and the exception thrown. The only next Behavior
I could pass would be the one inserted into that Activity
's Execute
method, but logically that means I'm compensating in the wrong direction as that next Behavior
is actually to be executed after this one succeeds, so I'd compensate too much.
I also tried to use the Catch
in the state machine, which does handle the exception, however, I couldn't find any way to start the execution of the compensation flow for the activity that failed when I only have the ExceptionActivityBinder
present.
Is there any good way to trigger the compensation flow of the activities?
An activity within a state machine (using Automatonymous) is much different than an activity within Courier. Unfortunately, they both have the same name, which can create confusion.
When an activity throws an exception, the Faulted
method of the next activity in the behavior is called. If that method is a regular activity method (such as .Then, .Publish, etc.) it is skipped, since the Faulted
method of those activities just calls the next activity in the behavior.
A Catch
activity, however, can be used to catch the exception and execute a rescue behavior (which is a sequence of activities).
Either way, the Faulted method of the activity which throws an exception within the Execute method is not called. So yes, you should use a try/catch, but allow the exception to flow back out of the Execute method so that the behavior handles it properly.