public static void RunSimpleSample() { var actionOne = new SimpleRollbackActionOne(); var actionTwo = new SimpleRollbackActionTwo(); using var scope = new RollbackScope <int>( continueRollbackOnFailed: true, rollbackParameter: 42 ); scope.Add(actionOne); scope.Add(actionTwo); scope.TryRollbackSafe(); }
public override bool MoveNext() { var statefulTaskWithRollback = _realEnumerator.Current; if (statefulTaskWithRollback is null) { throw GetStatefulTaskIsNullException(); } try { // Execute current task. bool canMoveNext = _realEnumerator.MoveNext(); // "Current" property can change -> add to scope the previous one. _rollbackScope.Add(statefulTaskWithRollback); // On final task clear rollback scope. if (!canMoveNext) { _rollbackScope.CommitAndClear(); } return(canMoveNext); } catch (Exception ex) { Logger.Exception( ex, "Failed to execute state machine. Trying to rollback completed tasks." ); _rollbackScope.TryRollbackSafe(); // Rethrow original exception to signalize callers that state machine execution is failed. throw; } }