internal Activity GetRootActivity(Type workflowType, bool createNew, bool initForRuntime)
        {
            Activity root = null;

            if (createNew)
            {
                return(LoadRootActivity(workflowType, false, initForRuntime));
            }
            bool exist;

            root = workflowTypes.GetOrGenerateDefinition(workflowType, null, null, null, initForRuntime, out exist);
            if (exist)
            {
                return(root);
            }
            // Set the locking object used for cloning the definition
            // for non-internal use (WorkflowInstance.GetWorkflowDefinition
            // and WorkflowCompletedEventArgs.WorkflowDefinition)
            WorkflowDefinitionLock.SetWorkflowDefinitionLockObject(root, new object());

            EventHandler <WorkflowDefinitionEventArgs> localWorkflowDefinitionLoaded = WorkflowDefinitionLoaded;

            if (localWorkflowDefinitionLoaded != null)
            {
                localWorkflowDefinitionLoaded(this.workflowRuntime, new WorkflowDefinitionEventArgs(workflowType));
            }

            return(root);
        }
        // This function will create a new root activity definition tree by deserializing the xoml and the rules file.
        // The last parameter createNew should be true when the caller is asking for a new definition for performing
        // dynamic updates instead of a cached definition.
        internal Activity GetRootActivity(string xomlText, string rulesText, bool createNew, bool initForRuntime)
        {
            if (string.IsNullOrEmpty(xomlText))
            {
                throw new ArgumentNullException("xomlText");
            }

            //calculate the "hash". Think 60s!
            byte[]       xomlHashCode    = null;
            MemoryStream xomlBytesStream = new MemoryStream();

            using (StreamWriter streamWriter = new StreamWriter(xomlBytesStream))
            {
                streamWriter.Write(xomlText);

                //consider rules, if they exist
                if (!string.IsNullOrEmpty(rulesText))
                {
                    streamWriter.Write(rulesText);
                }

                streamWriter.Flush();
                xomlBytesStream.Position = 0;

                xomlHashCode = MD5HashHelper.ComputeHash(xomlBytesStream.GetBuffer());
            }

            if (createNew)
            {
                return(LoadRootActivity(xomlText, rulesText, xomlHashCode, false, initForRuntime));
            }

            bool     exist;
            Activity root = xomlFragments.GetOrGenerateDefinition(null, xomlText, rulesText, xomlHashCode, initForRuntime, out exist);

            if (exist)
            {
                return(root);
            }
            // Set the locking object used for cloning the definition
            // for non-internal use (WorkflowInstance.GetWorkflowDefinition
            // and WorkflowCompletedEventArgs.WorkflowDefinition)
            WorkflowDefinitionLock.SetWorkflowDefinitionLockObject(root, new object());

            EventHandler <WorkflowDefinitionEventArgs> localWorkflowDefinitionLoaded = WorkflowDefinitionLoaded;

            if (localWorkflowDefinitionLoaded != null)
            {
                localWorkflowDefinitionLoaded(this.workflowRuntime, new WorkflowDefinitionEventArgs(xomlHashCode));
            }

            return(root);
        }
        internal Activity GetRootActivity(Type workflowType, bool createNew, bool initForRuntime)
        {
            Activity dependencyObject = null;
            bool     flag;

            if (createNew)
            {
                return(this.LoadRootActivity(workflowType, false, initForRuntime));
            }
            dependencyObject = this.workflowTypes.GetOrGenerateDefinition(workflowType, null, null, null, initForRuntime, out flag);
            if (!flag)
            {
                WorkflowDefinitionLock.SetWorkflowDefinitionLockObject(dependencyObject, new object());
                EventHandler <WorkflowDefinitionEventArgs> workflowDefinitionLoaded = this.WorkflowDefinitionLoaded;
                if (workflowDefinitionLoaded != null)
                {
                    workflowDefinitionLoaded(this.workflowRuntime, new WorkflowDefinitionEventArgs(workflowType));
                }
            }
            return(dependencyObject);
        }
        internal Activity GetRootActivity(string xomlText, string rulesText, bool createNew, bool initForRuntime)
        {
            bool flag;

            if (string.IsNullOrEmpty(xomlText))
            {
                throw new ArgumentNullException("xomlText");
            }
            byte[]       xomlHashCode = null;
            MemoryStream stream       = new MemoryStream();

            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.Write(xomlText);
                if (!string.IsNullOrEmpty(rulesText))
                {
                    writer.Write(rulesText);
                }
                writer.Flush();
                stream.Position = 0L;
                xomlHashCode    = MD5HashHelper.ComputeHash(stream.GetBuffer());
            }
            if (createNew)
            {
                return(this.LoadRootActivity(xomlText, rulesText, xomlHashCode, false, initForRuntime));
            }
            Activity dependencyObject = this.xomlFragments.GetOrGenerateDefinition(null, xomlText, rulesText, xomlHashCode, initForRuntime, out flag);

            if (!flag)
            {
                WorkflowDefinitionLock.SetWorkflowDefinitionLockObject(dependencyObject, new object());
                EventHandler <WorkflowDefinitionEventArgs> workflowDefinitionLoaded = this.WorkflowDefinitionLoaded;
                if (workflowDefinitionLoaded != null)
                {
                    workflowDefinitionLoaded(this.workflowRuntime, new WorkflowDefinitionEventArgs(xomlHashCode));
                }
            }
            return(dependencyObject);
        }
        // This function will create a new root activity definition tree by deserializing the xoml and the rules file.
        // The last parameter createNew should be true when the caller is asking for a new definition for performing
        // dynamic updates instead of a cached definition.
        internal Activity GetRootActivity(string xomlText, string rulesText, bool createNew, bool initForRuntime)
        {
            if (string.IsNullOrEmpty(xomlText))
            {
                throw new ArgumentNullException("xomlText");
            }

            //calculate the "hash". Think 60s!
            byte[]       xomlHashCode    = null;
            MemoryStream xomlBytesStream = new MemoryStream();

            using (StreamWriter streamWriter = new StreamWriter(xomlBytesStream))
            {
                streamWriter.Write(xomlText);

                //consider rules, if they exist
                if (!string.IsNullOrEmpty(rulesText))
                {
                    streamWriter.Write(rulesText);
                }

                streamWriter.Flush();
                xomlBytesStream.Position = 0;

                if (LocalAppContextSwitches.UseLegacyHashForWorkflowDefinitionDispenserCacheKey)
                {
                    xomlHashCode = MD5HashHelper.ComputeHash(xomlBytesStream.GetBuffer());
                }
                else
                {
                    SHA256 sha256Provider  = new SHA256CryptoServiceProvider();
                    byte[] xomlHashCode256 = sha256Provider.ComputeHash(xomlBytesStream.GetBuffer());
                    // The debugger "DigestComparer" expects 16 bytes.
                    xomlHashCode = new byte[16];
                    Array.Copy(xomlHashCode256, xomlHashCode, Math.Min(xomlHashCode.Length, xomlHashCode256.Length));
                }
            }

            if (createNew)
            {
                return(LoadRootActivity(xomlText, rulesText, xomlHashCode, false, initForRuntime));
            }

            bool     exist;
            Activity root = xomlFragments.GetOrGenerateDefinition(null, xomlText, rulesText, xomlHashCode, initForRuntime, out exist);

            if (exist)
            {
                return(root);
            }
            // Set the locking object used for cloning the definition
            // for non-internal use (WorkflowInstance.GetWorkflowDefinition
            // and WorkflowCompletedEventArgs.WorkflowDefinition)
            WorkflowDefinitionLock.SetWorkflowDefinitionLockObject(root, new object());

            EventHandler <WorkflowDefinitionEventArgs> localWorkflowDefinitionLoaded = WorkflowDefinitionLoaded;

            if (localWorkflowDefinitionLoaded != null)
            {
                localWorkflowDefinitionLoaded(this.workflowRuntime, new WorkflowDefinitionEventArgs(xomlHashCode));
            }

            return(root);
        }