// Initialize for the root schedule internal void Initialize(Activity rootActivity, WorkflowExecutor invokerExec, string invokeActivityID, Guid instanceId, IDictionary<string, object> namedArguments, WorkflowInstance workflowInstance) { this.rootActivity = rootActivity; this.InstanceId = instanceId; // Set the persisted State properties this.rootActivity.SetValue(WorkflowExecutor.ContextIdProperty, 0); this.rootActivity.SetValue(WorkflowInstanceIdProperty, instanceId); this.WorkflowStatus = WorkflowStatus.Created; this.rootActivity.SetValue(Activity.ActivityExecutionContextInfoProperty, new ActivityExecutionContextInfo(this.rootActivity.QualifiedName, GetNewContextId(), instanceId, -1)); this.rootActivity.SetValue(Activity.ActivityContextGuidProperty, instanceId); this.rootActivity.SetValue(WorkflowExecutor.IsIdleProperty, true); this.isInstanceIdle = true; // set workflow executor this.rootActivity.SetValue(WorkflowExecutor.WorkflowExecutorProperty, this); // initialize the root activity RefreshWorkflowDefinition(); Activity workflowDefinition = this.WorkflowDefinition; if (workflowDefinition == null) throw new InvalidOperationException("workflowDefinition"); ((IDependencyObjectAccessor)this.rootActivity).InitializeActivatingInstanceForRuntime(null, this); this.rootActivity.FixUpMetaProperties(workflowDefinition); _runtime = workflowInstance.WorkflowRuntime; if (invokerExec != null) { List<string> calleeBase = new List<string>(); TrackingCallingState parentTCS = (TrackingCallingState)invokerExec.rootActivity.GetValue(WorkflowExecutor.TrackingCallingStateProperty); if ((parentTCS != null) && (parentTCS.CallerActivityPathProxy != null)) { foreach (string qualifiedID in parentTCS.CallerActivityPathProxy) calleeBase.Add(qualifiedID); } calleeBase.Add(invokeActivityID); // // This has been exec'd by another instance // Set up tracking info to allow linking instances Debug.Assert(invokeActivityID != null && invokeActivityID.Length > 0); TrackingCallingState trackingCallingState = new TrackingCallingState(); trackingCallingState.CallerActivityPathProxy = calleeBase; trackingCallingState.CallerWorkflowInstanceId = invokerExec.InstanceId; trackingCallingState.CallerContextGuid = ((ActivityExecutionContextInfo)ContextActivityUtils.ContextActivity(invokerExec.CurrentActivity).GetValue(Activity.ActivityExecutionContextInfoProperty)).ContextGuid; if (null == invokerExec.CurrentActivity.Parent) trackingCallingState.CallerParentContextGuid = trackingCallingState.CallerContextGuid; else trackingCallingState.CallerParentContextGuid = ((ActivityExecutionContextInfo)ContextActivityUtils.ContextActivity(invokerExec.CurrentActivity.Parent).GetValue(Activity.ActivityExecutionContextInfoProperty)).ContextGuid; this.rootActivity.SetValue(WorkflowExecutor.TrackingCallingStateProperty, trackingCallingState); } _setInArgsOnCompanion(namedArguments); this.schedulingContext = new Scheduler(this, true); this._schedulerLock = LockFactory.CreateWorkflowSchedulerLock(this.InstanceId); qService = new WorkflowQueuingService(this); _workflowInstance = workflowInstance; TimerQueue = new TimerEventSubscriptionCollection(this, this.InstanceId); // register the dynamic activity using (new ServiceEnvironment(this.rootActivity)) { using (SetCurrentActivity(this.rootActivity)) { this.RegisterDynamicActivity(this.rootActivity, false); } } }
// Used to recreate the root schedule executor from its persisted state private void ReloadHelper(Activity rootActivity) { // assign activity state this.rootActivity = rootActivity; this.InstanceId = (Guid)rootActivity.GetValue(WorkflowInstanceIdProperty); // set workflow executor this.rootActivity.SetValue(WorkflowExecutor.WorkflowExecutorProperty, this); this._schedulerLock = LockFactory.CreateWorkflowSchedulerLock(this.InstanceId); this.schedulingContext = new Scheduler(this, false); this.qService = new WorkflowQueuingService(this); WorkflowTrace.Runtime.TraceEvent(TraceEventType.Information, 0, "Workflow Runtime: WorkflowExecutor: Loading instance {0}", this.InstanceIdString); DiagnosticStackTrace("load request"); using (new ServiceEnvironment(this.rootActivity)) { // check if this instance can be loaded switch (this.WorkflowStatus) { case WorkflowStatus.Completed: case WorkflowStatus.Terminated: WorkflowTrace.Runtime.TraceEvent(TraceEventType.Error, 0, "Workflow Runtime: WorkflowExecutor: attempt to load a completed/terminated instance: {0}", this.InstanceIdString); throw new InvalidOperationException( ExecutionStringManager.InvalidAttemptToLoad); default: break; } // new nonSerialized members _resourceManager = new VolatileResourceManager(); _runtime = _workflowInstance.WorkflowRuntime; // register all dynamic activities for loading Queue<Activity> dynamicActivitiesQueue = new Queue<Activity>(); dynamicActivitiesQueue.Enqueue(this.rootActivity); while (dynamicActivitiesQueue.Count > 0) { Activity dynamicActivity = dynamicActivitiesQueue.Dequeue(); ((IDependencyObjectAccessor)dynamicActivity).InitializeInstanceForRuntime(this); this.RegisterDynamicActivity(dynamicActivity, true); IList<Activity> nestedDynamicActivities = (IList<Activity>)dynamicActivity.GetValue(Activity.ActiveExecutionContextsProperty); if (nestedDynamicActivities != null) { foreach (Activity nestedDynamicActivity in nestedDynamicActivities) dynamicActivitiesQueue.Enqueue(nestedDynamicActivity); } } } this.isInstanceIdle = (bool)this.rootActivity.GetValue(IsIdleProperty); RefreshWorkflowDefinition(); }
void PrivateInitialize(Activity rootActivity, Guid instanceId, IList<PropertyInfo> outputProperties, Activity workflowDefinition) { this.instanceId = instanceId; this.rootActivity = rootActivity; this.contextActivityMap = new Dictionary<int, Activity>(); this.scheduler = new Scheduler(this); this.workflowQueuingService = new WorkflowQueuingService(this); this.outputProperties = outputProperties; this.resourceManager = new VolatileResourceManager(); this.rootActivity.SetValue(System.Workflow.ComponentModel.Activity.WorkflowDefinitionProperty, workflowDefinition); this.rootActivity.SetValue(WorkflowExecutor.WorkflowExecutorProperty, this); }