// 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);
                }
            }
        }
        private void WorkflowExecutorInitializing(object sender, WorkflowRuntime.WorkflowExecutorInitializingEventArgs e)
        {
            if (sender == null)
            {
                throw new ArgumentNullException("sender");
            }
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            if (!typeof(WorkflowExecutor).IsInstanceOfType(sender))
            {
                throw new ArgumentException("sender");
            }
            WorkflowExecutor skedExec = (WorkflowExecutor)sender;

            skedExec.WorkflowExecutionEvent += new EventHandler <WorkflowExecutor.WorkflowExecutionEventArgs>(this.WorkflowExecutionEvent);
            TrackingCallingState   trackingCallingState = skedExec.TrackingCallingState;
            TrackingListenerBroker broker = (TrackingListenerBroker)skedExec.RootActivity.GetValue(WorkflowExecutor.TrackingListenerBrokerProperty);

            if (broker != null)
            {
                broker.ReplaceServices(skedExec.WorkflowRuntime.TrackingServiceReplacement);
            }
            TrackingListener target    = null;
            WeakReference    reference = null;

            if (e.Loading)
            {
                bool flag = false;
                lock (this._listenerLock)
                {
                    flag = this._listeners.TryGetValue(skedExec.InstanceId, out reference);
                }
                if (flag)
                {
                    try
                    {
                        target = reference.Target as TrackingListener;
                    }
                    catch (InvalidOperationException)
                    {
                    }
                }
                if (target != null)
                {
                    target.Broker = broker;
                    goto Label_01C8;
                }
                target = this.GetTrackingListener(skedExec.WorkflowDefinition, skedExec, broker);
                if (target == null)
                {
                    goto Label_01C8;
                }
                if (reference != null)
                {
                    reference.Target = target;
                    goto Label_01C8;
                }
                lock (this._listenerLock)
                {
                    this._listeners.Add(skedExec.ID, new WeakReference(target));
                    goto Label_01C8;
                }
            }
            target = this.GetTrackingListener(skedExec.WorkflowDefinition, skedExec);
            if (target != null)
            {
                skedExec.RootActivity.SetValue(WorkflowExecutor.TrackingListenerBrokerProperty, target.Broker);
                lock (this._listenerLock)
                {
                    this._listeners.Add(skedExec.ID, new WeakReference(target));
                    goto Label_01C8;
                }
            }
            skedExec.RootActivity.SetValue(WorkflowExecutor.TrackingListenerBrokerProperty, new TrackingListenerBroker());
Label_01C8:
            if (target != null)
            {
                skedExec.WorkflowExecutionEvent += new EventHandler <WorkflowExecutor.WorkflowExecutionEventArgs>(target.WorkflowExecutionEvent);
            }
        }
        private List <TrackingChannelWrapper> GetChannels(Activity schedule, WorkflowExecutor exec, Guid instanceID, Type workflowType, ref TrackingListenerBroker broker)
        {
            if (this._services == null)
            {
                return(null);
            }
            bool flag = false;

            if (broker == null)
            {
                broker = new TrackingListenerBroker();
                flag   = true;
            }
            List <TrackingChannelWrapper> list             = new List <TrackingChannelWrapper>();
            List <string>          callPath                = null;
            Guid                   empty                   = Guid.Empty;
            Guid                   context                 = this.GetContext(exec.RootActivity);
            Guid                   callerContextGuid       = Guid.Empty;
            Guid                   callerParentContextGuid = Guid.Empty;
            TrackingCallingState   trackingCallingState    = exec.TrackingCallingState;
            TrackingListenerBroker broker1                 = (TrackingListenerBroker)exec.RootActivity.GetValue(WorkflowExecutor.TrackingListenerBrokerProperty);
            IList <string>         collection              = (trackingCallingState != null) ? trackingCallingState.CallerActivityPathProxy : null;

            if ((collection != null) && (collection.Count > 0))
            {
                callPath                = new List <string>(collection);
                empty                   = trackingCallingState.CallerWorkflowInstanceId;
                callerContextGuid       = trackingCallingState.CallerContextGuid;
                callerParentContextGuid = trackingCallingState.CallerParentContextGuid;
            }
            TrackingParameters parameters = new TrackingParameters(instanceID, workflowType, exec.WorkflowDefinition, callPath, empty, context, callerContextGuid, callerParentContextGuid);

            for (int i = 0; i < this._services.Count; i++)
            {
                TrackingChannel   trackingChannel     = null;
                Type              trackingServiceType = this._services[i].GetType();
                RTTrackingProfile profile             = null;
                if (flag)
                {
                    profile = this._profileManager.GetProfile(this._services[i], schedule);
                    if (profile == null)
                    {
                        continue;
                    }
                    broker.AddService(trackingServiceType, profile.Version);
                }
                else
                {
                    if (!broker.ContainsService(trackingServiceType))
                    {
                        continue;
                    }
                    if (broker.IsProfileInstance(trackingServiceType))
                    {
                        profile = this._profileManager.GetProfile(this._services[i], schedule, instanceID);
                        if (profile == null)
                        {
                            throw new InvalidOperationException(ExecutionStringManager.MissingProfileForService + trackingServiceType.ToString());
                        }
                        profile.IsPrivate = true;
                    }
                    else
                    {
                        Version version;
                        if (!broker.TryGetProfileVersionId(trackingServiceType, out version))
                        {
                            continue;
                        }
                        profile = this._profileManager.GetProfile(this._services[i], schedule, version);
                        if (profile == null)
                        {
                            throw new InvalidOperationException(ExecutionStringManager.MissingProfileForService + trackingServiceType.ToString() + ExecutionStringManager.MissingProfileForVersion + version.ToString());
                        }
                        if (broker.IsProfilePrivate(trackingServiceType))
                        {
                            profile           = profile.Clone();
                            profile.IsPrivate = true;
                        }
                    }
                }
                trackingChannel = this._services[i].GetTrackingChannel(parameters);
                if (trackingChannel == null)
                {
                    throw new InvalidOperationException(ExecutionStringManager.NullChannel);
                }
                list.Add(new TrackingChannelWrapper(trackingChannel, this._services[i].GetType(), workflowType, profile));
            }
            return(list);
        }
        internal void ReloadProfiles(WorkflowExecutor exec, Guid instanceId, ref TrackingListenerBroker broker, ref List <TrackingChannelWrapper> channels)
        {
            Type workflowType = exec.WorkflowDefinition.GetType();

            foreach (TrackingService service in this._services)
            {
                TrackingProfile        profile = null;
                TrackingChannelWrapper wrapper = null;
                if (service.TryReloadProfile(workflowType, instanceId, out profile))
                {
                    bool flag  = false;
                    int  index = 0;
                    while (index < channels.Count)
                    {
                        if (service.GetType() == channels[index].TrackingServiceType)
                        {
                            wrapper = channels[index];
                            flag    = true;
                            break;
                        }
                        index++;
                    }
                    if (profile == null)
                    {
                        if (flag)
                        {
                            broker.RemoveService(wrapper.TrackingServiceType);
                            channels.RemoveAt(index);
                        }
                    }
                    else
                    {
                        RTTrackingProfile profile2 = new RTTrackingProfile(profile, exec.WorkflowDefinition, workflowType)
                        {
                            IsPrivate = true
                        };
                        if (!flag)
                        {
                            List <string>        callPath                = null;
                            Guid                 empty                   = Guid.Empty;
                            TrackingCallingState trackingCallingState    = exec.TrackingCallingState;
                            IList <string>       collection              = null;
                            Guid                 context                 = this.GetContext(exec.RootActivity);
                            Guid                 callerContextGuid       = Guid.Empty;
                            Guid                 callerParentContextGuid = Guid.Empty;
                            if (trackingCallingState != null)
                            {
                                collection = trackingCallingState.CallerActivityPathProxy;
                                if ((collection != null) && (collection.Count > 0))
                                {
                                    callPath                = new List <string>(collection);
                                    empty                   = trackingCallingState.CallerWorkflowInstanceId;
                                    callerContextGuid       = trackingCallingState.CallerContextGuid;
                                    callerParentContextGuid = trackingCallingState.CallerParentContextGuid;
                                }
                            }
                            TrackingParameters     parameters = new TrackingParameters(instanceId, workflowType, exec.WorkflowDefinition, callPath, empty, context, callerContextGuid, callerParentContextGuid);
                            TrackingChannelWrapper item       = new TrackingChannelWrapper(service.GetTrackingChannel(parameters), service.GetType(), workflowType, profile2);
                            channels.Add(item);
                            Type type = service.GetType();
                            broker.AddService(type, profile2.Version);
                            broker.MakeProfileInstance(type);
                        }
                        else
                        {
                            wrapper.SetTrackingProfile(profile2);
                            broker.MakeProfileInstance(wrapper.TrackingServiceType);
                        }
                    }
                }
            }
        }