Exemplo n.º 1
0
        private ActivityExecutionStatus CompensateTargetActivity(ActivityExecutionContext context)
        {
            Activity activityByName = null;
            Activity activity2      = context.Activity;

            do
            {
                activityByName = activity2.Parent.GetActivityByName(this.TargetActivityName, true);
            }while (activityByName == null);
            if (((activityByName is ICompensatableActivity) && (activityByName.ExecutionStatus == ActivityExecutionStatus.Closed)) && (activityByName.ExecutionResult == ActivityExecutionResult.Succeeded))
            {
                activityByName.RegisterForStatusChange(Activity.ClosedEvent, this);
                context.CompensateActivity(activityByName);
                return(context.Activity.ExecutionStatus);
            }
            if (activityByName.ExecutionStatus == ActivityExecutionStatus.Initialized)
            {
                ActivityExecutionContextManager executionContextManager = context.ExecutionContextManager;
                foreach (ActivityExecutionContext context2 in executionContextManager.ExecutionContexts)
                {
                    if ((activityByName.GetActivityByName(context2.Activity.QualifiedName, true) != null) && (((context2.Activity.ExecutionStatus == ActivityExecutionStatus.Compensating) || (context2.Activity.ExecutionStatus == ActivityExecutionStatus.Faulting)) || (context2.Activity.ExecutionStatus == ActivityExecutionStatus.Canceling)))
                    {
                        return(context.Activity.ExecutionStatus);
                    }
                }
                for (int i = executionContextManager.CompletedExecutionContexts.Count - 1; i >= 0; i--)
                {
                    ActivityExecutionContextInfo contextInfo = executionContextManager.CompletedExecutionContexts[i];
                    if (((byte)(contextInfo.Flags & PersistFlags.NeedsCompensation)) != 0)
                    {
                        ActivityExecutionContext context3 = executionContextManager.DiscardPersistedExecutionContext(contextInfo);
                        if (context3.Activity is ICompensatableActivity)
                        {
                            context3.Activity.RegisterForStatusChange(Activity.ClosedEvent, this);
                            context3.CompensateActivity(context3.Activity);
                        }
                        return(context.Activity.ExecutionStatus);
                    }
                }
            }
            else if (CompensationUtils.TryCompensateLastCompletedChildActivity(context, activityByName, this))
            {
                return(context.Activity.ExecutionStatus);
            }
            return(ActivityExecutionStatus.Closed);
        }
        private ActivityExecutionStatus CompensateTargetActivity(ActivityExecutionContext context)
        {
            Activity targetActivity       = null;
            Activity commonParentActivity = context.Activity;

            do
            {
                commonParentActivity = commonParentActivity.Parent;
                targetActivity       = commonParentActivity.GetActivityByName(this.TargetActivityName, true);
            } while (targetActivity == null);

            if (targetActivity is ICompensatableActivity &&
                targetActivity.ExecutionStatus == ActivityExecutionStatus.Closed &&
                targetActivity.ExecutionResult == ActivityExecutionResult.Succeeded)
            {
                // same execution context
                targetActivity.RegisterForStatusChange(Activity.ClosedEvent, this);
                context.CompensateActivity(targetActivity);
                return(context.Activity.ExecutionStatus);
            }
            else if (targetActivity.ExecutionStatus == ActivityExecutionStatus.Initialized)
            {
                // Template activity

                // walk through active contexts
                ActivityExecutionContextManager contextManager = context.ExecutionContextManager;
                foreach (ActivityExecutionContext activeContext in contextManager.ExecutionContexts)
                {
                    if (targetActivity.GetActivityByName(activeContext.Activity.QualifiedName, true) != null)
                    {
                        if (activeContext.Activity.ExecutionStatus == ActivityExecutionStatus.Compensating ||
                            activeContext.Activity.ExecutionStatus == ActivityExecutionStatus.Faulting ||
                            activeContext.Activity.ExecutionStatus == ActivityExecutionStatus.Canceling
                            )
                        {
                            return(context.Activity.ExecutionStatus);
                        }
                    }
                }

                // walk through all completed execution contexts
                for (int index = contextManager.CompletedExecutionContexts.Count - 1; index >= 0; index--)
                {
                    //only compensate direct child during explicit compensation
                    ActivityExecutionContextInfo completedActivityInfo = contextManager.CompletedExecutionContexts[index];
                    if (((completedActivityInfo.Flags & PersistFlags.NeedsCompensation) != 0))
                    {
                        ActivityExecutionContext revokedExecutionContext = contextManager.DiscardPersistedExecutionContext(completedActivityInfo);
                        if (revokedExecutionContext.Activity is ICompensatableActivity)
                        {
                            revokedExecutionContext.Activity.RegisterForStatusChange(Activity.ClosedEvent, this);
                            revokedExecutionContext.CompensateActivity(revokedExecutionContext.Activity);
                        }
                        return(context.Activity.ExecutionStatus);
                    }
                }
            }
            else
            {
                // currently faulting, canceling, or compensating
                if (CompensationUtils.TryCompensateLastCompletedChildActivity(context, targetActivity, this))
                {
                    return(context.Activity.ExecutionStatus);
                }
            }
            return(ActivityExecutionStatus.Closed);
        }