IDictionary <XName, InstanceValue> GetCommandMetadata(out bool withIdentity)
        {
            CreateWorkflowOwnerWithIdentityCommand createOwnerWithIdentityCommand = base.InstancePersistenceCommand as CreateWorkflowOwnerWithIdentityCommand;

            if (createOwnerWithIdentityCommand != null)
            {
                withIdentity = true;
                return(createOwnerWithIdentityCommand.InstanceOwnerMetadata);
            }
            else
            {
                CreateWorkflowOwnerCommand createOwnerCommand = (CreateWorkflowOwnerCommand)base.InstancePersistenceCommand;
                withIdentity = false;
                return(createOwnerCommand.InstanceOwnerMetadata);
            }
        }
        public virtual void SetDefaultWorkflowInstanceOwner(XName workflowHostTypeName, IEnumerable<WorkflowIdentity> workflowIdentities)
        {
            if (Store.DefaultInstanceOwner == null || (_ownerInstanceHandle == null || _ownerInstanceHandle.IsValid == false))
            {
                lock (_syncLock)
                {
                    if (Store.DefaultInstanceOwner == null ||
                        (_ownerInstanceHandle == null || _ownerInstanceHandle.IsValid == false))
                    {
                        _workflowHostTypeName = workflowHostTypeName;
                        _workflowIdentities = workflowIdentities;
                        TryRenewInstanceHandle(ref _ownerInstanceHandle);

                        InstancePersistenceCommand createOwnerCmd = null;                        
#if NET4
                        createOwnerCmd = new CreateWorkflowOwnerCommand();
                        ((CreateWorkflowOwnerCommand)createOwnerCmd).InstanceOwnerMetadata.Add(WorkflowNamespaces.WorkflowHostTypePropertyName, new InstanceValue(workflowHostTypeName));
#else
                        if (workflowIdentities == null)
                        {
                            createOwnerCmd = new CreateWorkflowOwnerCommand();
                            ((CreateWorkflowOwnerCommand) createOwnerCmd).InstanceOwnerMetadata.Add(WorkflowNamespaces.WorkflowHostTypePropertyName, new InstanceValue(workflowHostTypeName));
                        }
                        else
                        {
                            // support workflow versioning
                            createOwnerCmd = new CreateWorkflowOwnerWithIdentityCommand();
                            ((CreateWorkflowOwnerWithIdentityCommand)createOwnerCmd).InstanceOwnerMetadata.Add(WorkflowNamespaces.WorkflowHostTypePropertyName, new InstanceValue(workflowHostTypeName));
                            ((CreateWorkflowOwnerWithIdentityCommand)createOwnerCmd).InstanceOwnerMetadata.Add(WorkflowNamespaces.DefinitionIdentityFilterName, new InstanceValue(WorkflowIdentityFilter.Any));
                            ((CreateWorkflowOwnerWithIdentityCommand)createOwnerCmd).InstanceOwnerMetadata.Add(WorkflowNamespaces.DefinitionIdentitiesName, new InstanceValue(workflowIdentities.ToList()));
                        }                                                                   
#endif
                        Store.DefaultInstanceOwner = ExecuteCommand(createOwnerCmd, DefaultTimeout).InstanceOwner;
                    }

                }
            }
        }
        InstancePersistenceCommand GetCreateOwnerCommand()
        {
            InstancePersistenceCommand command;
            IDictionary<XName, InstanceValue> commandMetadata;
            if (this.instanceOwnerMetadata.ContainsKey(Workflow45Namespace.DefinitionIdentities))
            {
                CreateWorkflowOwnerWithIdentityCommand withIdentity = new CreateWorkflowOwnerWithIdentityCommand();
                command = withIdentity;
                commandMetadata = withIdentity.InstanceOwnerMetadata;
            }
            else
            {
                CreateWorkflowOwnerCommand withoutIdentity = new CreateWorkflowOwnerCommand();
                command = withoutIdentity;
                commandMetadata = withoutIdentity.InstanceOwnerMetadata;
            }

            foreach (KeyValuePair<XName, InstanceValue> metadata in this.instanceOwnerMetadata)
            {
                commandMetadata.Add(metadata);
            }

            return command;
        }
        public static string GetIdentityMetadataXml(InstancePersistenceCommand command)
        {
            StringBuilder stringBuilder          = new StringBuilder(512);
            Guid          idHash                 = Guid.Empty;
            Guid          idAnyRevisionHash      = Guid.Empty;
            int           workflowIdentityFilter = (int)WorkflowIdentityFilter.Exact;

            IList <WorkflowIdentity> identityCollection = null;

            if (command is CreateWorkflowOwnerWithIdentityCommand)
            {
                InstanceValue instanceValueIdentityCollection       = null;
                CreateWorkflowOwnerWithIdentityCommand ownerCommand = command as CreateWorkflowOwnerWithIdentityCommand;

                if (ownerCommand.InstanceOwnerMetadata.TryGetValue(Workflow45Namespace.DefinitionIdentities, out instanceValueIdentityCollection))
                {
                    if (instanceValueIdentityCollection.Value != null)
                    {
                        identityCollection = instanceValueIdentityCollection.Value as IList <WorkflowIdentity>;
                        if (identityCollection == null)
                        {
                            string typeName = typeof(IList <>).Name.Replace("`1", "<" + typeof(WorkflowIdentity).Name + ">");
                            throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(SR.InvalidMetadataValue(Workflow45Namespace.DefinitionIdentities, typeName)));
                        }
                    }
                }

                InstanceValue instanceValue = null;
                if (ownerCommand.InstanceOwnerMetadata.TryGetValue(Workflow45Namespace.DefinitionIdentityFilter, out instanceValue))
                {
                    if (instanceValue.Value != null)
                    {
                        if (instanceValue.Value is WorkflowIdentityFilter)
                        {
                            workflowIdentityFilter = (int)instanceValue.Value;
                        }
                        else
                        {
                            workflowIdentityFilter = -1;
                        }

                        if (workflowIdentityFilter != (int)WorkflowIdentityFilter.Exact &&
                            workflowIdentityFilter != (int)WorkflowIdentityFilter.Any &&
                            workflowIdentityFilter != (int)WorkflowIdentityFilter.AnyRevision)
                        {
                            throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(SR.InvalidMetadataValue(Workflow45Namespace.DefinitionIdentityFilter, typeof(WorkflowIdentityFilter).Name)));
                        }
                    }
                }
            }
            else if (command is SaveWorkflowCommand)
            {
                InstanceValue       instanceValue = null;
                SaveWorkflowCommand saveCommand   = command as SaveWorkflowCommand;
                if (saveCommand.InstanceMetadataChanges.TryGetValue(Workflow45Namespace.DefinitionIdentity, out instanceValue))
                {
                    if (!instanceValue.IsDeletedValue && instanceValue.Value != null)
                    {
                        identityCollection = new Collection <WorkflowIdentity>();
                        if (!(instanceValue.Value is WorkflowIdentity))
                        {
                            throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(SR.InvalidMetadataValue(Workflow45Namespace.DefinitionIdentity, typeof(WorkflowIdentity).Name)));
                        }

                        identityCollection.Add((WorkflowIdentity)instanceValue.Value);
                    }
                }
                else
                {
                    // If identity isn't specified, we preserve the instance's existing identity
                    return(null);
                }
            }
            else
            {
                return(null);
            }

            if (identityCollection == null)
            {
                // Assume NULL Identity
                identityCollection = new Collection <WorkflowIdentity>();
                identityCollection.Add(null);
            }

            using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder))
            {
                xmlWriter.WriteStartDocument();
                xmlWriter.WriteStartElement("IdentityMetadata");

                // Write the Identity Collection
                xmlWriter.WriteStartElement("IdentityCollection");
                foreach (WorkflowIdentity id in identityCollection)
                {
                    xmlWriter.WriteStartElement("Identity");

                    if (id == null)
                    {
                        xmlWriter.WriteElementString("DefinitionIdentityHash", Guid.Empty.ToString());
                        xmlWriter.WriteElementString("DefinitionIdentityAnyRevisionHash", Guid.Empty.ToString());
                    }
                    else
                    {
                        idHash            = GetIdentityHash(id);
                        idAnyRevisionHash = GetIdentityAnyRevisionFilterHash(id);

                        xmlWriter.WriteElementString("DefinitionIdentityHash", idHash.ToString());
                        xmlWriter.WriteElementString("DefinitionIdentityAnyRevisionHash", idAnyRevisionHash.ToString());
                        xmlWriter.WriteElementString("Name", id.Name);

                        if (id.Package != null)
                        {
                            xmlWriter.WriteElementString("Package", id.Package);
                        }

                        if (id.Version != null)
                        {
                            xmlWriter.WriteElementString("Major", id.Version.Major.ToString(CultureInfo.InvariantCulture));
                            xmlWriter.WriteElementString("Minor", id.Version.Minor.ToString(CultureInfo.InvariantCulture));
                            if (id.Version.Build >= 0)
                            {
                                xmlWriter.WriteElementString("Build", id.Version.Build.ToString(CultureInfo.InvariantCulture));
                                if (id.Version.Revision >= 0)
                                {
                                    xmlWriter.WriteElementString("Revision", id.Version.Revision.ToString(CultureInfo.InvariantCulture));
                                }
                            }
                        }
                    }

                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();

                // Write the WorkflowIdentityFilter
                xmlWriter.WriteElementString("WorkflowIdentityFilter", workflowIdentityFilter.ToString(CultureInfo.InvariantCulture));

                xmlWriter.WriteEndElement();
            }
            return(stringBuilder.ToString());
        }