internal static void InitializeCorrelationTokenCollection(Activity activity, CorrelationToken correlator) { if ((correlator != null) && !string.IsNullOrEmpty(correlator.OwnerActivityName)) { string ownerActivityName = correlator.OwnerActivityName; Activity activityByName = activity.GetActivityByName(ownerActivityName); if (activityByName == null) { activityByName = Helpers.ParseActivityForBind(activity, ownerActivityName); } if (activityByName == null) { throw new ArgumentException("ownerActivity"); } CorrelationTokenCollection tokens = activityByName.GetValue(CorrelationTokenCollection.CorrelationTokenCollectionProperty) as CorrelationTokenCollection; if (tokens == null) { tokens = new CorrelationTokenCollection(); activityByName.SetValue(CorrelationTokenCollection.CorrelationTokenCollectionProperty, tokens); } if (!tokens.Contains(correlator.Name)) { tokens.Add(correlator); } } }
private static void CollectCompensatableCompletedContexts(ActivityExecutionContext context, Activity targetActivity, SortedDictionary<int, CompensationInfo> sortedListOfCompensatableTargets, bool immediateCompensation) { ActivityExecutionContextManager executionContextManager = context.ExecutionContextManager; for (int i = executionContextManager.CompletedExecutionContexts.Count - 1; i >= 0; i--) { ActivityExecutionContextInfo targetExecutionInfo = executionContextManager.CompletedExecutionContexts[i]; if (((byte) (targetExecutionInfo.Flags & PersistFlags.NeedsCompensation)) != 0) { Activity activityByName = targetActivity.GetActivityByName(targetExecutionInfo.ActivityQualifiedName, true); if ((activityByName != null) && (!immediateCompensation || !IsActivityInBackWorkBranch(targetActivity, activityByName))) { sortedListOfCompensatableTargets.Add(targetExecutionInfo.CompletedOrderId, new CompensationInfo(targetExecutionInfo, executionContextManager)); } } } }
public override bool Evaluate(Activity activity, IServiceProvider provider) { if (activity == null) { throw new ArgumentNullException("activity"); } if (string.IsNullOrEmpty(this._condition)) { throw new InvalidOperationException(SR.GetString("Error_MissingConditionName", new object[] { activity.Name })); } RuleDefinitions ruleDefinitions = null; if (string.IsNullOrEmpty(this.declaringActivityId)) { CompositeActivity declaringActivity = null; ruleDefinitions = GetRuleDefinitions(activity, out declaringActivity); } else { ruleDefinitions = (RuleDefinitions) activity.GetActivityByName(this.declaringActivityId).GetValue(RuleDefinitions.RuleDefinitionsProperty); } if ((ruleDefinitions == null) || (ruleDefinitions.Conditions == null)) { throw new InvalidOperationException(SR.GetString("Error_MissingRuleConditions")); } RuleCondition condition = ruleDefinitions.Conditions[this._condition]; if (condition == null) { return true; } Activity enclosingActivity = Helpers.GetEnclosingActivity(activity); RuleValidation validation = new RuleValidation(enclosingActivity); if (!condition.Validate(validation)) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ConditionValidationFailed, new object[] { this._condition })); } RuleExecution execution = new RuleExecution(validation, enclosingActivity, provider as ActivityExecutionContext); return condition.Evaluate(execution); }
internal static void InitializeCorrelationTokenCollection(Activity activity, CorrelationToken correlator) { if (correlator != null && !String.IsNullOrEmpty(correlator.OwnerActivityName)) { string ownerActivityId = correlator.OwnerActivityName; Activity owner = activity.GetActivityByName(ownerActivityId); if (owner == null) owner = System.Workflow.Activities.Common.Helpers.ParseActivityForBind(activity, ownerActivityId); if (owner == null) throw new ArgumentException("ownerActivity"); CorrelationTokenCollection collection = owner.GetValue(CorrelationTokenCollection.CorrelationTokenCollectionProperty) as CorrelationTokenCollection; if (collection == null) { collection = new CorrelationTokenCollection(); owner.SetValue(CorrelationTokenCollection.CorrelationTokenCollectionProperty, collection); } if (!collection.Contains(correlator.Name)) { collection.Add(correlator); } } }
private static bool CollectCompensatableActiveContexts(ActivityExecutionContext context, Activity targetActivity, SortedDictionary<int, CompensationInfo> sortedListOfCompensatableTargets, bool immediateCompensation) { foreach (ActivityExecutionContext context2 in context.ExecutionContextManager.ExecutionContexts) { if ((targetActivity.GetActivityByName(context2.Activity.QualifiedName, true) != null) && (!immediateCompensation || !IsActivityInBackWorkBranch(targetActivity, context2.Activity))) { if ((context2.Activity is ICompensatableActivity) && (((context2.Activity.ExecutionStatus == ActivityExecutionStatus.Compensating) || (context2.Activity.ExecutionStatus == ActivityExecutionStatus.Faulting)) || (context2.Activity.ExecutionStatus == ActivityExecutionStatus.Canceling))) { return true; } if (context2.Activity is CompositeActivity) { Activity[] compensatableChildren = GetCompensatableChildren(context2.Activity as CompositeActivity); if (compensatableChildren != null) { int key = 0; foreach (Activity activity in compensatableChildren) { int num2 = (int) activity.GetValue(Activity.CompletedOrderIdProperty); if (key < num2) { key = num2; } } if (key != 0) { sortedListOfCompensatableTargets.Add(key, new CompensationInfo(context2)); } } CollectCompensatableActiveContexts(context2, targetActivity, sortedListOfCompensatableTargets, immediateCompensation); CollectCompensatableCompletedContexts(context2, targetActivity, sortedListOfCompensatableTargets, immediateCompensation); } } } return false; }
private static void CompleteRevokedExecutionContext(Activity targetActivity, ActivityExecutionContext context) { ActivityExecutionContext[] array = new ActivityExecutionContext[context.ExecutionContextManager.ExecutionContexts.Count]; context.ExecutionContextManager.ExecutionContexts.CopyTo(array, 0); foreach (ActivityExecutionContext context2 in array) { if (targetActivity.GetActivityByName(context2.Activity.QualifiedName, true) != null) { if (context2.Activity.ExecutionStatus == ActivityExecutionStatus.Closed) { CompleteRevokedExecutionContext(context2.Activity, context2); } context.ExecutionContextManager.CompleteExecutionContext(context2); } } }
private static bool IsOwnerActivitySame(string ownerActivityName, string existingOwnerActivityName, Activity currentActivity, Activity existingActivity) { if (ownerActivityName.Equals(existingOwnerActivityName)) { return true; } Activity activityByName = currentActivity.GetActivityByName(ownerActivityName); if (activityByName == null) { activityByName = Helpers.ParseActivityForBind(currentActivity, ownerActivityName); } Activity activity2 = currentActivity.GetActivityByName(existingOwnerActivityName); if (activity2 == null) { activity2 = Helpers.ParseActivityForBind(existingActivity, existingOwnerActivityName); } return (((activityByName != null) && (activity2 != null)) && activityByName.QualifiedName.Equals(activity2.QualifiedName)); }
public override bool Evaluate(Activity activity, IServiceProvider provider) { if (activity == null) { throw new ArgumentNullException("activity"); } if (string.IsNullOrEmpty(this._condition)) { throw new InvalidOperationException(SR.GetString(SR.Error_MissingConditionName, activity.Name)); } RuleDefinitions defs = null; if (string.IsNullOrEmpty(this.declaringActivityId)) { // No Runtime Initialization. CompositeActivity declaringActivity = null; defs = RuleConditionReference.GetRuleDefinitions(activity, out declaringActivity); } else { // Runtime Initialized. defs = (RuleDefinitions)activity.GetActivityByName(declaringActivityId).GetValue(RuleDefinitions.RuleDefinitionsProperty); } if ((defs == null) || (defs.Conditions == null)) { throw new InvalidOperationException(SR.GetString(SR.Error_MissingRuleConditions)); } RuleCondition conditionDefinitionToEvaluate = defs.Conditions[this._condition]; if (conditionDefinitionToEvaluate != null) { Activity contextActivity = System.Workflow.Activities.Common.Helpers.GetEnclosingActivity(activity); RuleValidation validation = new RuleValidation(contextActivity); if (!conditionDefinitionToEvaluate.Validate(validation)) { string message = string.Format(CultureInfo.CurrentCulture, Messages.ConditionValidationFailed, this._condition); throw new InvalidOperationException(message); } RuleExecution context = new RuleExecution(validation, contextActivity, provider as ActivityExecutionContext); return conditionDefinitionToEvaluate.Evaluate(context); } else { // no condition, so defaults to true return true; } }
private static bool IsOwnerActivitySame(string ownerActivityName, string existingOwnerActivityName, Activity currentActivity, Activity existingActivity) { if (ownerActivityName.Equals(existingOwnerActivityName)) return true; Activity owner = currentActivity.GetActivityByName(ownerActivityName); if (owner == null) owner = Helpers.ParseActivityForBind(currentActivity, ownerActivityName); Activity existingowner = currentActivity.GetActivityByName(existingOwnerActivityName); if (existingowner == null) existingowner = Helpers.ParseActivityForBind(existingActivity, existingOwnerActivityName); if (owner != null && existingowner != null && owner.QualifiedName.Equals(existingowner.QualifiedName)) return true; return false; }
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); }
public override bool Run(IWorkflowCoreRuntime workflowCoreRuntime) { // get state reader Activity contextActivity = workflowCoreRuntime.GetContextActivityForId(this.ContextId); Activity activity = contextActivity.GetActivityByName(this.activityName); using (workflowCoreRuntime.SetCurrentActivity(activity)) { using (ActivityExecutionContext activityExecutionContext = new ActivityExecutionContext(activity)) { ActivityExecutor activityExecutor = ActivityExecutors.GetActivityExecutor(activity); switch (this.operation) { case ActivityOperationType.Execute: if (activity.ExecutionStatus == ActivityExecutionStatus.Executing) { try { workflowCoreRuntime.RaiseActivityExecuting(activity); ActivityExecutionStatus newStatus = activityExecutor.Execute(activity, activityExecutionContext); if (newStatus == ActivityExecutionStatus.Closed) { activityExecutionContext.CloseActivity(); } else if (newStatus != ActivityExecutionStatus.Executing) { throw new InvalidOperationException(SR.GetString(SR.InvalidExecutionStatus, activity.QualifiedName, newStatus.ToString(), ActivityExecutionStatus.Executing.ToString())); } } catch (Exception e) { System.Workflow.Runtime.WorkflowTrace.Runtime.TraceEvent(TraceEventType.Error, 1, "Execute of Activity {0} threw {1}", activity.QualifiedName, e.ToString()); throw; } } break; case ActivityOperationType.Cancel: if (activity.ExecutionStatus == ActivityExecutionStatus.Canceling) { try { ActivityExecutionStatus newStatus = activityExecutor.Cancel(activity, activityExecutionContext); if (newStatus == ActivityExecutionStatus.Closed) { activityExecutionContext.CloseActivity(); } else if (newStatus != ActivityExecutionStatus.Canceling) { throw new InvalidOperationException(SR.GetString(SR.InvalidExecutionStatus, activity.QualifiedName, newStatus.ToString(), ActivityExecutionStatus.Canceling.ToString())); } } catch (Exception e) { System.Workflow.Runtime.WorkflowTrace.Runtime.TraceEvent(TraceEventType.Error, 1, "Cancel of Activity {0} threw {1}", activity.QualifiedName, e.ToString()); throw; } } break; case ActivityOperationType.Compensate: if (activity.ExecutionStatus == ActivityExecutionStatus.Compensating) { try { ActivityExecutionStatus newStatus = activityExecutor.Compensate(activity, activityExecutionContext); if (newStatus == ActivityExecutionStatus.Closed) { activityExecutionContext.CloseActivity(); } else if (newStatus != ActivityExecutionStatus.Compensating) { throw new InvalidOperationException(SR.GetString(SR.InvalidExecutionStatus, activity.QualifiedName, newStatus.ToString(), ActivityExecutionStatus.Compensating.ToString())); } } catch (Exception e) { System.Workflow.Runtime.WorkflowTrace.Runtime.TraceEvent(TraceEventType.Error, 1, "Compensate of Activity {0} threw {1}", activity.QualifiedName, e.ToString()); throw; } } break; case ActivityOperationType.HandleFault: if (activity.ExecutionStatus == ActivityExecutionStatus.Faulting) { try { ActivityExecutionStatus newStatus = activityExecutor.HandleFault(activity, activityExecutionContext, this.exceptionToDeliver); if (newStatus == ActivityExecutionStatus.Closed) { activityExecutionContext.CloseActivity(); } else if (newStatus != ActivityExecutionStatus.Faulting) { throw new InvalidOperationException(SR.GetString(SR.InvalidExecutionStatus, activity.QualifiedName, newStatus.ToString(), ActivityExecutionStatus.Faulting.ToString())); } } catch (Exception e) { System.Workflow.Runtime.WorkflowTrace.Runtime.TraceEvent(TraceEventType.Error, 1, "Compensate of Activity {0} threw {1}", activity.QualifiedName, e.ToString()); throw; } } break; } } } return(true); }