protected void GenerateLoadSqlCommand
        (
            SqlCommand command,
            LoadType loadType,
            Guid keyToLoadBy,
            Guid instanceId,
            List <CorrelationKey> keysToAssociate
        )
        {
            long surrogateLockOwnerId = base.StoreLock.SurrogateLockOwnerId;

            byte[] concatenatedKeyProperties = null;
            bool   singleKeyToAssociate      = (keysToAssociate != null && keysToAssociate.Count == 1);

            if (keysToAssociate != null)
            {
                concatenatedKeyProperties = SerializationUtilities.CreateKeyBinaryBlob(keysToAssociate);
            }

            double operationTimeout = this.TimeoutHelper.RemainingTime().TotalMilliseconds;

            SqlParameterCollection parameters = command.Parameters;

            parameters.Add(new SqlParameter {
                ParameterName = "@surrogateLockOwnerId", SqlDbType = SqlDbType.BigInt, Value = surrogateLockOwnerId
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@operationType", SqlDbType = SqlDbType.TinyInt, Value = loadType
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@keyToLoadBy", SqlDbType = SqlDbType.UniqueIdentifier, Value = keyToLoadBy
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@instanceId", SqlDbType = SqlDbType.UniqueIdentifier, Value = instanceId
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@handleInstanceVersion", SqlDbType = SqlDbType.BigInt, Value = base.InstancePersistenceContext.InstanceVersion
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@handleIsBoundToLock", SqlDbType = SqlDbType.Bit, Value = base.InstancePersistenceContext.InstanceView.IsBoundToLock
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@keysToAssociate", SqlDbType = SqlDbType.Xml, Value = singleKeyToAssociate ? DBNull.Value : SerializationUtilities.CreateCorrelationKeyXmlBlob(keysToAssociate)
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@encodingOption", SqlDbType = SqlDbType.TinyInt, Value = base.Store.InstanceEncodingOption
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@concatenatedKeyProperties", SqlDbType = SqlDbType.VarBinary, Value = (object)concatenatedKeyProperties ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@operationTimeout", SqlDbType = SqlDbType.Int, Value = (operationTimeout < Int32.MaxValue) ? Convert.ToInt32(operationTimeout) : Int32.MaxValue
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@singleKeyId", SqlDbType = SqlDbType.UniqueIdentifier, Value = singleKeyToAssociate ? keysToAssociate[0].KeyId : (object)DBNull.Value
            });
        }
Esempio n. 2
0
 private void ReadKeyData(SqlDataReader reader, Dictionary <Guid, IDictionary <XName, InstanceValue> > associatedInstanceKeys, Dictionary <Guid, IDictionary <XName, InstanceValue> > completedInstanceKeys)
 {
     if ((StoreUtilities.GetNextResultSet(base.InstancePersistenceCommand.Name, reader) == null) && !reader.IsDBNull(1))
     {
         do
         {
             Guid guid    = reader.GetGuid(1);
             bool boolean = reader.GetBoolean(2);
             InstanceEncodingOption @byte = (InstanceEncodingOption)reader.GetByte(3);
             Dictionary <Guid, IDictionary <XName, InstanceValue> > dictionary = boolean ? associatedInstanceKeys : completedInstanceKeys;
             if (!reader.IsDBNull(4))
             {
                 dictionary[guid] = SerializationUtilities.DeserializeKeyMetadata((byte[])reader.GetValue(4), @byte);
             }
             else
             {
                 dictionary[guid] = new Dictionary <XName, InstanceValue>();
             }
         }while (reader.Read());
     }
 }
Esempio n. 3
0
 private void ReadInstanceMetadataChanges(SqlDataReader reader, Dictionary <XName, InstanceValue> instanceMetadata)
 {
     if ((StoreUtilities.GetNextResultSet(base.InstancePersistenceCommand.Name, reader) != null) || !reader.IsDBNull(1))
     {
         do
         {
             InstanceEncodingOption @byte        = (InstanceEncodingOption)reader.GetByte(1);
             byte[] serializedMetadataProperties = (byte[])reader.GetValue(2);
             foreach (KeyValuePair <XName, InstanceValue> pair in SerializationUtilities.DeserializeMetadataPropertyBag(serializedMetadataProperties, @byte))
             {
                 XName         key    = pair.Key;
                 InstanceValue value2 = pair.Value;
                 if (value2.Value is DeletedMetadataValue)
                 {
                     instanceMetadata.Remove(key);
                 }
                 else
                 {
                     instanceMetadata[key] = value2;
                 }
             }
         }while (reader.Read());
     }
 }
Esempio n. 4
0
        protected void GenerateLoadSqlCommand(SqlCommand command, LoadType loadType, Guid keyToLoadBy, Guid instanceId, List <CorrelationKey> keysToAssociate)
        {
            long surrogateLockOwnerId = base.StoreLock.SurrogateLockOwnerId;

            byte[] buffer = null;
            bool   flag   = (keysToAssociate != null) && (keysToAssociate.Count == 1);

            if (keysToAssociate != null)
            {
                buffer = SerializationUtilities.CreateKeyBinaryBlob(keysToAssociate);
            }
            double totalMilliseconds          = base.TimeoutHelper.RemainingTime().TotalMilliseconds;
            SqlParameterCollection parameters = command.Parameters;
            SqlParameter           parameter  = new SqlParameter {
                ParameterName = "@surrogateLockOwnerId",
                SqlDbType     = SqlDbType.BigInt,
                Value         = surrogateLockOwnerId
            };

            parameters.Add(parameter);
            SqlParameter parameter2 = new SqlParameter {
                ParameterName = "@operationType",
                SqlDbType     = SqlDbType.TinyInt,
                Value         = loadType
            };

            parameters.Add(parameter2);
            SqlParameter parameter3 = new SqlParameter {
                ParameterName = "@keyToLoadBy",
                SqlDbType     = SqlDbType.UniqueIdentifier,
                Value         = keyToLoadBy
            };

            parameters.Add(parameter3);
            SqlParameter parameter4 = new SqlParameter {
                ParameterName = "@instanceId",
                SqlDbType     = SqlDbType.UniqueIdentifier,
                Value         = instanceId
            };

            parameters.Add(parameter4);
            SqlParameter parameter5 = new SqlParameter {
                ParameterName = "@handleInstanceVersion",
                SqlDbType     = SqlDbType.BigInt,
                Value         = base.InstancePersistenceContext.InstanceVersion
            };

            parameters.Add(parameter5);
            SqlParameter parameter6 = new SqlParameter {
                ParameterName = "@handleIsBoundToLock",
                SqlDbType     = SqlDbType.Bit,
                Value         = base.InstancePersistenceContext.InstanceView.IsBoundToLock
            };

            parameters.Add(parameter6);
            SqlParameter parameter7 = new SqlParameter {
                ParameterName = "@keysToAssociate",
                SqlDbType     = SqlDbType.Xml,
                Value         = flag ? DBNull.Value : SerializationUtilities.CreateCorrelationKeyXmlBlob(keysToAssociate)
            };

            parameters.Add(parameter7);
            SqlParameter parameter8 = new SqlParameter {
                ParameterName = "@encodingOption",
                SqlDbType     = SqlDbType.TinyInt,
                Value         = base.Store.InstanceEncodingOption
            };

            parameters.Add(parameter8);
            SqlParameter parameter9 = new SqlParameter {
                ParameterName = "@concatenatedKeyProperties",
                SqlDbType     = SqlDbType.VarBinary,
                Value         = buffer ?? DBNull.Value
            };

            parameters.Add(parameter9);
            SqlParameter parameter10 = new SqlParameter {
                ParameterName = "@operationTimeout",
                SqlDbType     = SqlDbType.Int,
                Value         = (totalMilliseconds < 2147483647.0) ? Convert.ToInt32(totalMilliseconds) : 0x7fffffff
            };

            parameters.Add(parameter10);
            SqlParameter parameter11 = new SqlParameter {
                ParameterName = "@singleKeyId",
                SqlDbType     = SqlDbType.UniqueIdentifier,
                Value         = flag ? ((object)keysToAssociate[0].KeyId) : ((object)DBNull.Value)
            };

            parameters.Add(parameter11);
        }
Esempio n. 5
0
        protected override Exception ProcessSqlResult(SqlDataReader reader)
        {
            Exception nextResultSet = StoreUtilities.GetNextResultSet(base.InstancePersistenceCommand.Name, reader);

            if (nextResultSet == null)
            {
                Guid instanceId = reader.GetGuid(1);
                reader.GetInt64(2);
                byte[] primitiveDataProperties                = reader.IsDBNull(3) ? null : ((byte[])reader.GetValue(3));
                byte[] complexDataProperties                  = reader.IsDBNull(4) ? null : ((byte[])reader.GetValue(4));
                byte[] serializedMetadataProperties           = reader.IsDBNull(5) ? null : ((byte[])reader.GetValue(5));
                InstanceEncodingOption @byte                  = (InstanceEncodingOption)reader.GetByte(6);
                InstanceEncodingOption instanceEncodingOption = (InstanceEncodingOption)reader.GetByte(7);
                long instanceVersion = reader.GetInt64(8);
                bool boolean         = reader.GetBoolean(9);
                bool flag2           = reader.GetBoolean(10);
                InstancePersistenceCommand       instancePersistenceCommand = base.InstancePersistenceCommand;
                LoadWorkflowByInstanceKeyCommand command = base.InstancePersistenceCommand as LoadWorkflowByInstanceKeyCommand;
                if (!base.InstancePersistenceContext.InstanceView.IsBoundToInstance)
                {
                    base.InstancePersistenceContext.BindInstance(instanceId);
                }
                if (!base.InstancePersistenceContext.InstanceView.IsBoundToInstanceOwner)
                {
                    base.InstancePersistenceContext.BindInstanceOwner(base.StoreLock.LockOwnerId, base.StoreLock.LockOwnerId);
                }
                if (!base.InstancePersistenceContext.InstanceView.IsBoundToLock)
                {
                    ((InstanceLockTracking)base.InstancePersistenceContext.UserContext).TrackStoreLock(instanceId, instanceVersion, base.DependentTransaction);
                    base.InstancePersistenceContext.BindAcquiredLock(instanceVersion);
                }
                this.instanceData     = SerializationUtilities.DeserializePropertyBag(primitiveDataProperties, complexDataProperties, @byte);
                this.instanceMetadata = SerializationUtilities.DeserializeMetadataPropertyBag(serializedMetadataProperties, instanceEncodingOption);
                if (!flag2)
                {
                    this.ReadInstanceMetadataChanges(reader, this.instanceMetadata);
                    this.ReadKeyData(reader, this.associatedInstanceKeys, this.completedInstanceKeys);
                }
                else if (command != null)
                {
                    foreach (KeyValuePair <Guid, IDictionary <XName, InstanceValue> > pair in command.InstanceKeysToAssociate)
                    {
                        this.associatedInstanceKeys.Add(pair.Key, pair.Value);
                    }
                    if (!this.associatedInstanceKeys.ContainsKey(command.LookupInstanceKey))
                    {
                        base.InstancePersistenceContext.AssociatedInstanceKey(command.LookupInstanceKey);
                        this.associatedInstanceKeys.Add(command.LookupInstanceKey, new Dictionary <XName, InstanceValue>());
                    }
                }
                if (command != null)
                {
                    foreach (KeyValuePair <Guid, IDictionary <XName, InstanceValue> > pair2 in command.InstanceKeysToAssociate)
                    {
                        base.InstancePersistenceContext.AssociatedInstanceKey(pair2.Key);
                        if (pair2.Value != null)
                        {
                            foreach (KeyValuePair <XName, InstanceValue> pair3 in pair2.Value)
                            {
                                base.InstancePersistenceContext.WroteInstanceKeyMetadataValue(pair2.Key, pair3.Key, pair3.Value);
                            }
                        }
                    }
                }
                base.InstancePersistenceContext.LoadedInstance(boolean ? InstanceState.Initialized : InstanceState.Uninitialized, this.instanceData, this.instanceMetadata, this.associatedInstanceKeys, this.completedInstanceKeys);
                return(nextResultSet);
            }
            if (nextResultSet is InstanceLockLostException)
            {
                base.InstancePersistenceContext.InstanceHandle.Free();
            }
            return(nextResultSet);
        }
        protected override void GenerateSqlCommand(SqlCommand sqlCommand)
        {
            base.GenerateSqlCommand(sqlCommand);

            if (base.StoreLock.IsValid)
            {
                throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(SR.MultipleLockOwnersNotSupported));
            }

            bool withIdentity;
            IDictionary <XName, InstanceValue> commandMetadata = GetCommandMetadata(out withIdentity);
            SqlParameterCollection             parameters      = sqlCommand.Parameters;
            double lockTimeout = base.Store.BufferedHostLockRenewalPeriod.TotalSeconds;

            this.lockOwnerId = Guid.NewGuid();
            ExtractWorkflowHostType(commandMetadata);

            InstanceValue instanceValue;

            if (commandMetadata.TryGetValue(PersistenceMetadataNamespace.ActivationType, out instanceValue))
            {
                if (withIdentity)
                {
                    throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(SR.IdentityNotSupportedWithActivation));
                }
                if (!PersistenceMetadataNamespace.ActivationTypes.WAS.Equals(instanceValue.Value))
                {
                    throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(SR.NonWASActivationNotSupported));
                }
                this.fireActivatableInstancesEvent = true;
            }

            ArraySegment <byte>[] properties = SerializationUtilities.SerializePropertyBag(commandMetadata, base.Store.InstanceEncodingOption);

            parameters.Add(new SqlParameter {
                ParameterName = "@lockTimeout", SqlDbType = SqlDbType.Int, Value = lockTimeout
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@lockOwnerId", SqlDbType = SqlDbType.UniqueIdentifier, Value = this.lockOwnerId
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@workflowHostType", SqlDbType = SqlDbType.UniqueIdentifier, Value = (base.Store.WorkflowHostType != Guid.Empty) ? base.Store.WorkflowHostType : (object)DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@enqueueCommand", SqlDbType = SqlDbType.Bit, Value = base.Store.EnqueueRunCommands
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@deleteInstanceOnCompletion", SqlDbType = SqlDbType.Bit, Value = (base.Store.InstanceCompletionAction == InstanceCompletionAction.DeleteAll)
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@primitiveLockOwnerData", SqlDbType = SqlDbType.VarBinary, Size = properties[0].Count, Value = (object)(properties[0].Array) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@complexLockOwnerData", SqlDbType = SqlDbType.VarBinary, Size = properties[1].Count, Value = (object)(properties[1].Array) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@writeOnlyPrimitiveLockOwnerData", SqlDbType = SqlDbType.VarBinary, Size = properties[2].Count, Value = (object)(properties[2].Array) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@writeOnlyComplexLockOwnerData", SqlDbType = SqlDbType.VarBinary, Size = properties[3].Count, Value = (object)(properties[3].Array) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@encodingOption", SqlDbType = SqlDbType.TinyInt, Value = base.Store.InstanceEncodingOption
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@machineName", SqlDbType = SqlDbType.NVarChar, Value = SqlWorkflowInstanceStoreConstants.MachineName
            });

            if (withIdentity)
            {
                Fx.Assert(base.Store.DatabaseVersion >= StoreUtilities.Version45, "Should never get here if the db version isn't 4.5 or higher");

                string identityMetadataXml = SerializationUtilities.GetIdentityMetadataXml(base.InstancePersistenceCommand);
                parameters.Add(new SqlParameter {
                    ParameterName = "@identityMetadata", SqlDbType = SqlDbType.Xml, Value = identityMetadataXml
                });
            }
        }
Esempio n. 7
0
 public CorrelationKey(Guid keyId, IDictionary <XName, InstanceValue> keyMetadata, InstanceEncodingOption encodingOption)
 {
     this.KeyId      = keyId;
     this.BinaryData = SerializationUtilities.SerializeKeyMetadata(keyMetadata, encodingOption);
 }
        protected override void GenerateSqlCommand(SqlCommand sqlCommand)
        {
            InstanceValue value2;

            base.GenerateSqlCommand(sqlCommand);
            if (base.StoreLock.IsValid)
            {
                throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(System.Activities.DurableInstancing.SR.MultipleLockOwnersNotSupported));
            }
            CreateWorkflowOwnerCommand instancePersistenceCommand = base.InstancePersistenceCommand as CreateWorkflowOwnerCommand;
            SqlParameterCollection     parameters = sqlCommand.Parameters;
            double totalSeconds = base.Store.BufferedHostLockRenewalPeriod.TotalSeconds;

            this.lockOwnerId = Guid.NewGuid();
            this.ExtractWorkflowHostType();
            if (instancePersistenceCommand.InstanceOwnerMetadata.TryGetValue(PersistenceMetadataNamespace.ActivationType, out value2))
            {
                if (!PersistenceMetadataNamespace.ActivationTypes.WAS.Equals(value2.Value))
                {
                    throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(System.Activities.DurableInstancing.SR.NonWASActivationNotSupported));
                }
                this.fireActivatableInstancesEvent = true;
            }
            ArraySegment <byte>[] segmentArray = SerializationUtilities.SerializePropertyBag(instancePersistenceCommand.InstanceOwnerMetadata, base.Store.InstanceEncodingOption);
            SqlParameter          parameter    = new SqlParameter {
                ParameterName = "@lockTimeout",
                SqlDbType     = SqlDbType.Int,
                Value         = totalSeconds
            };

            parameters.Add(parameter);
            SqlParameter parameter2 = new SqlParameter {
                ParameterName = "@lockOwnerId",
                SqlDbType     = SqlDbType.UniqueIdentifier,
                Value         = this.lockOwnerId
            };

            parameters.Add(parameter2);
            SqlParameter parameter3 = new SqlParameter {
                ParameterName = "@workflowHostType",
                SqlDbType     = SqlDbType.UniqueIdentifier,
                Value         = (base.Store.WorkflowHostType != Guid.Empty) ? ((object)base.Store.WorkflowHostType) : ((object)DBNull.Value)
            };

            parameters.Add(parameter3);
            SqlParameter parameter4 = new SqlParameter {
                ParameterName = "@enqueueCommand",
                SqlDbType     = SqlDbType.Bit,
                Value         = base.Store.EnqueueRunCommands
            };

            parameters.Add(parameter4);
            SqlParameter parameter5 = new SqlParameter {
                ParameterName = "@deleteInstanceOnCompletion",
                SqlDbType     = SqlDbType.Bit,
                Value         = base.Store.InstanceCompletionAction == InstanceCompletionAction.DeleteAll
            };

            parameters.Add(parameter5);
            SqlParameter parameter6 = new SqlParameter {
                ParameterName = "@primitiveLockOwnerData",
                SqlDbType     = SqlDbType.VarBinary,
                Size          = segmentArray[0].Count,
                Value         = segmentArray[0].Array ?? DBNull.Value
            };

            parameters.Add(parameter6);
            SqlParameter parameter7 = new SqlParameter {
                ParameterName = "@complexLockOwnerData",
                SqlDbType     = SqlDbType.VarBinary,
                Size          = segmentArray[1].Count,
                Value         = segmentArray[1].Array ?? DBNull.Value
            };

            parameters.Add(parameter7);
            SqlParameter parameter8 = new SqlParameter {
                ParameterName = "@writeOnlyPrimitiveLockOwnerData",
                SqlDbType     = SqlDbType.VarBinary,
                Size          = segmentArray[2].Count,
                Value         = segmentArray[2].Array ?? DBNull.Value
            };

            parameters.Add(parameter8);
            SqlParameter parameter9 = new SqlParameter {
                ParameterName = "@writeOnlyComplexLockOwnerData",
                SqlDbType     = SqlDbType.VarBinary,
                Size          = segmentArray[3].Count,
                Value         = segmentArray[3].Array ?? DBNull.Value
            };

            parameters.Add(parameter9);
            SqlParameter parameter10 = new SqlParameter {
                ParameterName = "@encodingOption",
                SqlDbType     = SqlDbType.TinyInt,
                Value         = base.Store.InstanceEncodingOption
            };

            parameters.Add(parameter10);
            SqlParameter parameter11 = new SqlParameter {
                ParameterName = "@machineName",
                SqlDbType     = SqlDbType.NVarChar,
                Value         = SqlWorkflowInstanceStoreConstants.MachineName
            };

            parameters.Add(parameter11);
        }
        protected override Exception ProcessSqlResult(SqlDataReader reader)
        {
            Exception exception = StoreUtilities.GetNextResultSet(base.InstancePersistenceCommand.Name, reader);

            if (exception == null)
            {
                Guid   instanceId          = reader.GetGuid(1);
                long   surrogateInstanceId = reader.GetInt64(2);
                byte[] primitiveProperties = reader.IsDBNull(3) ? null : (byte[])(reader.GetValue(3));
                byte[] complexProperties   = reader.IsDBNull(4) ? null : (byte[])(reader.GetValue(4));
                byte[] metadataProperties  = reader.IsDBNull(5) ? null : (byte[])(reader.GetValue(5));
                InstanceEncodingOption dataEncodingOption     = (InstanceEncodingOption)(reader.GetByte(6));
                InstanceEncodingOption metadataEncodingOption = (InstanceEncodingOption)(reader.GetByte(7));
                long version         = reader.GetInt64(8);
                bool isInitialized   = reader.GetBoolean(9);
                bool createdInstance = reader.GetBoolean(10);

                LoadWorkflowCommand loadWorkflowCommand           = base.InstancePersistenceCommand as LoadWorkflowCommand;
                LoadWorkflowByInstanceKeyCommand loadByKeycommand = base.InstancePersistenceCommand as LoadWorkflowByInstanceKeyCommand;

                if (!base.InstancePersistenceContext.InstanceView.IsBoundToInstance)
                {
                    base.InstancePersistenceContext.BindInstance(instanceId);
                }
                if (!base.InstancePersistenceContext.InstanceView.IsBoundToInstanceOwner)
                {
                    base.InstancePersistenceContext.BindInstanceOwner(base.StoreLock.LockOwnerId, base.StoreLock.LockOwnerId);
                }
                if (!base.InstancePersistenceContext.InstanceView.IsBoundToLock)
                {
                    InstanceLockTracking instanceLockTracking = (InstanceLockTracking)(base.InstancePersistenceContext.UserContext);
                    instanceLockTracking.TrackStoreLock(instanceId, version, this.DependentTransaction);
                    base.InstancePersistenceContext.BindAcquiredLock(version);
                }

                this.instanceData     = SerializationUtilities.DeserializePropertyBag(primitiveProperties, complexProperties, dataEncodingOption);
                this.instanceMetadata = SerializationUtilities.DeserializeMetadataPropertyBag(metadataProperties, metadataEncodingOption);

                if (!createdInstance)
                {
                    ReadInstanceMetadataChanges(reader, this.instanceMetadata);
                    ReadKeyData(reader, this.associatedInstanceKeys, this.completedInstanceKeys);
                }
                else if (loadByKeycommand != null)
                {
                    foreach (KeyValuePair <Guid, IDictionary <XName, InstanceValue> > keyEntry in loadByKeycommand.InstanceKeysToAssociate)
                    {
                        this.associatedInstanceKeys.Add(keyEntry.Key, keyEntry.Value);
                    }

                    if (!this.associatedInstanceKeys.ContainsKey(loadByKeycommand.LookupInstanceKey))
                    {
                        base.InstancePersistenceContext.AssociatedInstanceKey(loadByKeycommand.LookupInstanceKey);
                        this.associatedInstanceKeys.Add(loadByKeycommand.LookupInstanceKey, new Dictionary <XName, InstanceValue>());
                    }
                }

                if (loadByKeycommand != null)
                {
                    foreach (KeyValuePair <Guid, IDictionary <XName, InstanceValue> > keyEntry in loadByKeycommand.InstanceKeysToAssociate)
                    {
                        base.InstancePersistenceContext.AssociatedInstanceKey(keyEntry.Key);

                        if (keyEntry.Value != null)
                        {
                            foreach (KeyValuePair <XName, InstanceValue> property in keyEntry.Value)
                            {
                                base.InstancePersistenceContext.WroteInstanceKeyMetadataValue(keyEntry.Key, property.Key, property.Value);
                            }
                        }
                    }
                }

                base.InstancePersistenceContext.LoadedInstance
                (
                    isInitialized ? InstanceState.Initialized : InstanceState.Uninitialized,
                    this.instanceData,
                    this.instanceMetadata,
                    this.associatedInstanceKeys,
                    this.completedInstanceKeys
                );
            }
            else if (exception is InstanceLockLostException)
            {
                base.InstancePersistenceContext.InstanceHandle.Free();
            }

            return(exception);
        }
Esempio n. 10
0
        private void SerializePromotedProperties(SqlParameterCollection parameters, StringBuilder commandTextBuilder, SaveWorkflowCommand saveWorkflowCommand)
        {
            int num = 0;

            foreach (KeyValuePair <string, Tuple <List <XName>, List <XName> > > pair in base.Store.Promotions)
            {
                StringBuilder builder = new StringBuilder(0x200);
                int           num2    = 1;
                bool          flag    = false;
                string        str     = string.Format(CultureInfo.InvariantCulture, "@promotionName{0}", new object[] { num });
                string        str2    = string.Format(CultureInfo.InvariantCulture, "@instanceId{0}", new object[] { num });
                builder.Append(string.Format(CultureInfo.InvariantCulture, "exec {0}.[InsertPromotedProperties] ", new object[] { "[System.Activities.DurableInstancing]" }));
                builder.Append("@promotionName=");
                builder.Append(str);
                builder.Append(",");
                builder.Append("@instanceId=");
                builder.Append(str2);
                foreach (XName name in pair.Value.Item1)
                {
                    InstanceValue value2;
                    if (saveWorkflowCommand.InstanceData.TryGetValue(name, out value2))
                    {
                        if (!SerializationUtilities.IsPropertyTypeSqlVariantCompatible(value2))
                        {
                            throw FxTrace.Exception.AsError(new InstancePersistenceException(System.Activities.DurableInstancing.SR.CannotPromoteAsSqlVariant(value2.Value.GetType().ToString(), name.ToString())));
                        }
                        string       str3      = string.Format(CultureInfo.InvariantCulture, "@value{0}=", new object[] { num2 });
                        string       str4      = string.Format(CultureInfo.InvariantCulture, "@value{0}_promotion{1}", new object[] { num2, num });
                        SqlParameter parameter = new SqlParameter {
                            SqlDbType     = SqlDbType.Variant,
                            ParameterName = str4,
                            Value         = value2.Value ?? DBNull.Value
                        };
                        parameters.Add(parameter);
                        builder.Append(", ");
                        builder.Append(str3);
                        builder.Append(str4);
                        flag = true;
                    }
                    num2++;
                }
                num2 = 0x21;
                foreach (XName name2 in pair.Value.Item2)
                {
                    InstanceValue     value3;
                    IObjectSerializer objectSerializer = ObjectSerializerFactory.GetObjectSerializer(base.Store.InstanceEncodingOption);
                    if (saveWorkflowCommand.InstanceData.TryGetValue(name2, out value3))
                    {
                        string str5          = string.Format(CultureInfo.InvariantCulture, "@value{0}=", new object[] { num2 });
                        string parameterName = string.Format(CultureInfo.InvariantCulture, "@value{0}_promotion{1}", new object[] { num2, num });
                        AddSerializedProperty(objectSerializer.SerializeValue(value3.Value), parameters, parameterName);
                        builder.Append(", ");
                        builder.Append(str5);
                        builder.Append(parameterName);
                        flag = true;
                    }
                    num2++;
                }
                if (flag)
                {
                    SqlParameter parameter2 = new SqlParameter {
                        SqlDbType     = SqlDbType.NVarChar,
                        Size          = 400,
                        ParameterName = str,
                        Value         = pair.Key
                    };
                    parameters.Add(parameter2);
                    SqlParameter parameter3 = new SqlParameter {
                        SqlDbType     = SqlDbType.UniqueIdentifier,
                        ParameterName = str2,
                        Value         = base.InstancePersistenceContext.InstanceView.InstanceId
                    };
                    parameters.Add(parameter3);
                    builder.Append(";");
                    commandTextBuilder.AppendLine(builder.ToString());
                    num++;
                }
            }
        }
Esempio n. 11
0
 private void SerializeAssociatedData(SqlParameterCollection parameters, SaveWorkflowCommand saveWorkflowCommand, StringBuilder commandTextBuilder)
 {
     if (saveWorkflowCommand.CompleteInstance && (base.Store.InstanceCompletionAction == InstanceCompletionAction.DeleteAll))
     {
         SqlParameter parameter = new SqlParameter {
             ParameterName = "@keysToAssociate",
             SqlDbType     = SqlDbType.Xml,
             Value         = DBNull.Value
         };
         parameters.Add(parameter);
         SqlParameter parameter2 = new SqlParameter {
             ParameterName = "@singleKeyId",
             SqlDbType     = SqlDbType.UniqueIdentifier,
             Value         = DBNull.Value
         };
         parameters.Add(parameter2);
         SqlParameter parameter3 = new SqlParameter {
             ParameterName = "@keysToComplete",
             SqlDbType     = SqlDbType.Xml,
             Value         = DBNull.Value
         };
         parameters.Add(parameter3);
         SqlParameter parameter4 = new SqlParameter {
             ParameterName = "@keysToFree",
             SqlDbType     = SqlDbType.Xml,
             Value         = DBNull.Value
         };
         parameters.Add(parameter4);
         SqlParameter parameter5 = new SqlParameter {
             ParameterName = "@concatenatedKeyProperties",
             SqlDbType     = SqlDbType.VarBinary,
             Value         = DBNull.Value
         };
         parameters.Add(parameter5);
         SqlParameter parameter6 = new SqlParameter {
             ParameterName = "@primitiveDataProperties",
             SqlDbType     = SqlDbType.VarBinary,
             Value         = DBNull.Value
         };
         parameters.Add(parameter6);
         SqlParameter parameter7 = new SqlParameter {
             ParameterName = "@complexDataProperties",
             SqlDbType     = SqlDbType.VarBinary,
             Value         = DBNull.Value
         };
         parameters.Add(parameter7);
         SqlParameter parameter8 = new SqlParameter {
             ParameterName = "@writeOnlyPrimitiveDataProperties",
             SqlDbType     = SqlDbType.VarBinary,
             Value         = DBNull.Value
         };
         parameters.Add(parameter8);
         SqlParameter parameter9 = new SqlParameter {
             ParameterName = "@writeOnlyComplexDataProperties",
             SqlDbType     = SqlDbType.VarBinary,
             Value         = DBNull.Value
         };
         parameters.Add(parameter9);
         SqlParameter parameter10 = new SqlParameter {
             ParameterName = "@metadataProperties",
             SqlDbType     = SqlDbType.VarBinary,
             Value         = DBNull.Value
         };
         parameters.Add(parameter10);
         SqlParameter parameter11 = new SqlParameter {
             ParameterName = "@metadataIsConsistent",
             SqlDbType     = SqlDbType.Bit,
             Value         = DBNull.Value
         };
         parameters.Add(parameter11);
         SqlParameter parameter12 = new SqlParameter {
             ParameterName = "@encodingOption",
             SqlDbType     = SqlDbType.TinyInt,
             Value         = DBNull.Value
         };
         parameters.Add(parameter12);
         SqlParameter parameter13 = new SqlParameter {
             ParameterName = "@lastMachineRunOn",
             SqlDbType     = SqlDbType.NVarChar,
             Value         = DBNull.Value
         };
         parameters.Add(parameter13);
         SqlParameter parameter14 = new SqlParameter {
             ParameterName = "@executionStatus",
             SqlDbType     = SqlDbType.NVarChar,
             Value         = DBNull.Value
         };
         parameters.Add(parameter14);
         SqlParameter parameter15 = new SqlParameter {
             ParameterName = "@blockingBookmarks",
             SqlDbType     = SqlDbType.NVarChar,
             Value         = DBNull.Value
         };
         parameters.Add(parameter15);
     }
     else
     {
         List <CorrelationKey> correlationKeys = CorrelationKey.BuildKeyList(saveWorkflowCommand.InstanceKeysToAssociate, base.Store.InstanceEncodingOption);
         List <CorrelationKey> list2           = CorrelationKey.BuildKeyList(saveWorkflowCommand.InstanceKeysToComplete);
         List <CorrelationKey> list3           = CorrelationKey.BuildKeyList(saveWorkflowCommand.InstanceKeysToFree);
         ArraySegment <byte>[] segmentArray    = SerializationUtilities.SerializePropertyBag(saveWorkflowCommand.InstanceData, base.Store.InstanceEncodingOption);
         ArraySegment <byte>   segment         = SerializationUtilities.SerializeMetadataPropertyBag(saveWorkflowCommand, base.InstancePersistenceContext, base.Store.InstanceEncodingOption);
         byte[]       buffer      = SerializationUtilities.CreateKeyBinaryBlob(correlationKeys);
         bool         flag        = base.InstancePersistenceContext.InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None;
         bool         flag2       = (correlationKeys != null) && (correlationKeys.Count == 1);
         SqlParameter parameter16 = new SqlParameter {
             ParameterName = "@keysToAssociate",
             SqlDbType     = SqlDbType.Xml,
             Value         = flag2 ? DBNull.Value : SerializationUtilities.CreateCorrelationKeyXmlBlob(correlationKeys)
         };
         parameters.Add(parameter16);
         SqlParameter parameter17 = new SqlParameter {
             ParameterName = "@singleKeyId",
             SqlDbType     = SqlDbType.UniqueIdentifier,
             Value         = flag2 ? ((object)correlationKeys[0].KeyId) : ((object)DBNull.Value)
         };
         parameters.Add(parameter17);
         SqlParameter parameter18 = new SqlParameter {
             ParameterName = "@keysToComplete",
             SqlDbType     = SqlDbType.Xml,
             Value         = SerializationUtilities.CreateCorrelationKeyXmlBlob(list2)
         };
         parameters.Add(parameter18);
         SqlParameter parameter19 = new SqlParameter {
             ParameterName = "@keysToFree",
             SqlDbType     = SqlDbType.Xml,
             Value         = SerializationUtilities.CreateCorrelationKeyXmlBlob(list3)
         };
         parameters.Add(parameter19);
         SqlParameter parameter20 = new SqlParameter {
             ParameterName = "@concatenatedKeyProperties",
             SqlDbType     = SqlDbType.VarBinary,
             Size          = -1,
             Value         = buffer ?? DBNull.Value
         };
         parameters.Add(parameter20);
         SqlParameter parameter21 = new SqlParameter {
             ParameterName = "@metadataIsConsistent",
             SqlDbType     = SqlDbType.Bit,
             Value         = flag
         };
         parameters.Add(parameter21);
         SqlParameter parameter22 = new SqlParameter {
             ParameterName = "@encodingOption",
             SqlDbType     = SqlDbType.TinyInt,
             Value         = base.Store.InstanceEncodingOption
         };
         parameters.Add(parameter22);
         SqlParameter parameter23 = new SqlParameter {
             ParameterName = "@lastMachineRunOn",
             SqlDbType     = SqlDbType.NVarChar,
             Size          = 450,
             Value         = SqlWorkflowInstanceStoreConstants.MachineName
         };
         parameters.Add(parameter23);
         SqlParameter parameter24 = new SqlParameter {
             ParameterName = "@executionStatus",
             SqlDbType     = SqlDbType.NVarChar,
             Size          = 450,
             Value         = GetExecutionStatus(saveWorkflowCommand) ?? DBNull.Value
         };
         parameters.Add(parameter24);
         SqlParameter parameter25 = new SqlParameter {
             ParameterName = "@blockingBookmarks",
             SqlDbType     = SqlDbType.NVarChar,
             Size          = -1,
             Value         = GetBlockingBookmarks(saveWorkflowCommand) ?? DBNull.Value
         };
         parameters.Add(parameter25);
         ArraySegment <byte>[] segmentArray2 = new ArraySegment <byte>[] { segmentArray[0], segmentArray[1], segmentArray[2], segmentArray[3], segment };
         string[] strArray = new string[] { "@primitiveDataProperties", "@complexDataProperties", "@writeOnlyPrimitiveDataProperties", "writeOnlyComplexDataProperties", "@metadataProperties" };
         for (int i = 0; i < 5; i++)
         {
             AddSerializedProperty(segmentArray2[i], parameters, strArray[i]);
         }
         this.SerializePromotedProperties(parameters, commandTextBuilder, saveWorkflowCommand);
     }
 }
        protected override void GenerateSqlCommand(SqlCommand command)
        {
            SaveWorkflowCommand saveWorkflowCommand = base.InstancePersistenceCommand as SaveWorkflowCommand;
            StringBuilder       commandTextBuilder  = new StringBuilder(SqlWorkflowInstanceStoreConstants.DefaultStringBuilderCapacity);
            double operationTimeout           = this.TimeoutHelper.RemainingTime().TotalMilliseconds;
            SqlParameterCollection parameters = command.Parameters;
            string suspensionReason;
            string suspensionExceptionName;

            parameters.Add(new SqlParameter {
                ParameterName = "@instanceId", SqlDbType = SqlDbType.UniqueIdentifier, Value = base.InstancePersistenceContext.InstanceView.InstanceId
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@surrogateLockOwnerId", SqlDbType = SqlDbType.BigInt, Value = base.StoreLock.SurrogateLockOwnerId
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@handleInstanceVersion", SqlDbType = SqlDbType.BigInt, Value = base.InstancePersistenceContext.InstanceVersion
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@handleIsBoundToLock", SqlDbType = SqlDbType.Bit, Value = base.InstancePersistenceContext.InstanceView.IsBoundToLock
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@timerDurationMilliseconds", SqlDbType = SqlDbType.BigInt, Value = (object)GetPendingTimerExpiration(saveWorkflowCommand) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@unlockInstance", SqlDbType = SqlDbType.Bit, Value = saveWorkflowCommand.UnlockInstance
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@suspensionStateChange", SqlDbType = SqlDbType.TinyInt, Value = GetSuspensionReason(saveWorkflowCommand, out suspensionReason, out suspensionExceptionName)
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@suspensionReason", SqlDbType = SqlDbType.NVarChar, Value = (object)suspensionReason ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@suspensionExceptionName", SqlDbType = SqlDbType.NVarChar, Size = 450, Value = (object)suspensionExceptionName ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@isCompleted", SqlDbType = SqlDbType.Bit, Value = saveWorkflowCommand.CompleteInstance
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@isReadyToRun", SqlDbType = SqlDbType.Bit, Value = IsReadyToRun(saveWorkflowCommand)
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@workflowHostType", SqlDbType = SqlDbType.UniqueIdentifier, Value = (object)GetWorkflowHostType(saveWorkflowCommand) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@operationTimeout", SqlDbType = SqlDbType.Int, Value = (operationTimeout < Int32.MaxValue) ? Convert.ToInt32(operationTimeout) : Int32.MaxValue
            });

            string parameterNames = null;

            if (base.Store.DatabaseVersion >= StoreUtilities.Version45)
            {
                string identityMetadataXml = SerializationUtilities.GetIdentityMetadataXml(saveWorkflowCommand);
                parameters.Add(new SqlParameter {
                    ParameterName = "@identityMetadata", SqlDbType = SqlDbType.Xml, Value = (object)identityMetadataXml ?? DBNull.Value
                });

                parameterNames = SaveWorkflowAsyncResult.storedProcedureParameters;
            }
            else
            {
                parameterNames = SaveWorkflowAsyncResult.storedProcedureParameters40;
            }

            commandTextBuilder.AppendLine(@"set nocount on
                                            set transaction isolation level read committed		
                                            set xact_abort on
                                            begin transaction");

            ExtractServiceDeploymentInformation(saveWorkflowCommand, commandTextBuilder, parameters);

            commandTextBuilder.AppendLine("declare @result int");
            commandTextBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "exec @result = {0}.[SaveInstance] {1} ;",
                                                        SqlWorkflowInstanceStoreConstants.DefaultSchema, parameterNames));
            commandTextBuilder.AppendLine("if (@result = 0)");
            commandTextBuilder.AppendLine("begin");

            SerializeAssociatedData(parameters, saveWorkflowCommand, commandTextBuilder);

            commandTextBuilder.AppendLine("commit transaction");
            commandTextBuilder.AppendLine("end");
            commandTextBuilder.AppendLine("else");
            commandTextBuilder.AppendLine("rollback transaction");

            this.commandText = commandTextBuilder.ToString();
        }
        void SerializePromotedProperties(SqlParameterCollection parameters, StringBuilder commandTextBuilder, SaveWorkflowCommand saveWorkflowCommand)
        {
            const int    SqlVariantStartColumn  = 1;
            const string promotionNameParameter = "@promotionName=";
            const string instanceIdParameter    = "@instanceId=";
            int          promotionNumber        = 0;

            foreach (KeyValuePair <string, Tuple <List <XName>, List <XName> > > promotion in base.Store.Promotions)
            {
                StringBuilder storedProcInvocationBuilder = new StringBuilder(SqlWorkflowInstanceStoreConstants.DefaultStringBuilderCapacity);
                int           column                = SqlVariantStartColumn;
                bool          addPromotion          = false;
                string        promotionNameArgument = string.Format(CultureInfo.InvariantCulture, "@promotionName{0}", promotionNumber);
                string        instanceIdArgument    = string.Format(CultureInfo.InvariantCulture, "@instanceId{0}", promotionNumber);

                storedProcInvocationBuilder.Append(string.Format(CultureInfo.InvariantCulture, "exec {0}.[InsertPromotedProperties] ", SqlWorkflowInstanceStoreConstants.DefaultSchema));
                storedProcInvocationBuilder.Append(promotionNameParameter);
                storedProcInvocationBuilder.Append(promotionNameArgument);
                storedProcInvocationBuilder.Append(",");
                storedProcInvocationBuilder.Append(instanceIdParameter);
                storedProcInvocationBuilder.Append(instanceIdArgument);

                foreach (XName name in promotion.Value.Item1)
                {
                    InstanceValue propertyValue;

                    if (saveWorkflowCommand.InstanceData.TryGetValue(name, out propertyValue))
                    {
                        if (!SerializationUtilities.IsPropertyTypeSqlVariantCompatible(propertyValue))
                        {
                            throw FxTrace.Exception.AsError(new InstancePersistenceException(SR.CannotPromoteAsSqlVariant(propertyValue.Value.GetType().ToString(), name.ToString())));
                        }

                        string parameterName = string.Format(CultureInfo.InvariantCulture, "@value{0}=", column);
                        string argumentName  = string.Format(CultureInfo.InvariantCulture, "@value{0}_promotion{1}", column, promotionNumber);
                        parameters.Add(new SqlParameter()
                        {
                            SqlDbType = SqlDbType.Variant, ParameterName = argumentName, Value = propertyValue.Value ?? DBNull.Value
                        });

                        storedProcInvocationBuilder.Append(", ");
                        storedProcInvocationBuilder.Append(parameterName);
                        storedProcInvocationBuilder.Append(argumentName);
                        addPromotion = true;
                    }
                    column++;
                }

                column = SqlVariantStartColumn + SqlWorkflowInstanceStoreConstants.MaximumPropertiesPerPromotion;

                foreach (XName name in promotion.Value.Item2)
                {
                    InstanceValue     propertyValue;
                    IObjectSerializer serializer = ObjectSerializerFactory.GetObjectSerializer(base.Store.InstanceEncodingOption);

                    if (saveWorkflowCommand.InstanceData.TryGetValue(name, out propertyValue))
                    {
                        string parameterName = string.Format(CultureInfo.InvariantCulture, "@value{0}=", column);
                        string argumentName  = string.Format(CultureInfo.InvariantCulture, "@value{0}_promotion{1}", column, promotionNumber);

                        SaveWorkflowAsyncResult.AddSerializedProperty(serializer.SerializeValue(propertyValue.Value), parameters, argumentName);
                        storedProcInvocationBuilder.Append(", ");
                        storedProcInvocationBuilder.Append(parameterName);
                        storedProcInvocationBuilder.Append(argumentName);
                        addPromotion = true;
                    }
                    column++;
                }

                if (addPromotion)
                {
                    parameters.Add(new SqlParameter()
                    {
                        SqlDbType = SqlDbType.NVarChar, Size = 400, ParameterName = promotionNameArgument, Value = promotion.Key
                    });
                    parameters.Add(new SqlParameter()
                    {
                        SqlDbType = SqlDbType.UniqueIdentifier, ParameterName = instanceIdArgument, Value = base.InstancePersistenceContext.InstanceView.InstanceId
                    });
                    storedProcInvocationBuilder.Append(";");
                    commandTextBuilder.AppendLine(storedProcInvocationBuilder.ToString());
                    promotionNumber++;
                }
            }
        }
        void SerializeAssociatedData(SqlParameterCollection parameters, SaveWorkflowCommand saveWorkflowCommand, StringBuilder commandTextBuilder)
        {
            if (saveWorkflowCommand.CompleteInstance && base.Store.InstanceCompletionAction == InstanceCompletionAction.DeleteAll)
            {
                parameters.Add(new SqlParameter {
                    ParameterName = "@keysToAssociate", SqlDbType = SqlDbType.Xml, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@singleKeyId", SqlDbType = SqlDbType.UniqueIdentifier, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@keysToComplete", SqlDbType = SqlDbType.Xml, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@keysToFree", SqlDbType = SqlDbType.Xml, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@concatenatedKeyProperties", SqlDbType = SqlDbType.VarBinary, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@primitiveDataProperties", SqlDbType = SqlDbType.VarBinary, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@complexDataProperties", SqlDbType = SqlDbType.VarBinary, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@writeOnlyPrimitiveDataProperties", SqlDbType = SqlDbType.VarBinary, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@writeOnlyComplexDataProperties", SqlDbType = SqlDbType.VarBinary, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@metadataProperties", SqlDbType = SqlDbType.VarBinary, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@metadataIsConsistent", SqlDbType = SqlDbType.Bit, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@encodingOption", SqlDbType = SqlDbType.TinyInt, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@lastMachineRunOn", SqlDbType = SqlDbType.NVarChar, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@executionStatus", SqlDbType = SqlDbType.NVarChar, Value = DBNull.Value
                });
                parameters.Add(new SqlParameter {
                    ParameterName = "@blockingBookmarks", SqlDbType = SqlDbType.NVarChar, Value = DBNull.Value
                });

                return;
            }

            List <CorrelationKey> keysToAssociate = CorrelationKey.BuildKeyList(saveWorkflowCommand.InstanceKeysToAssociate, base.Store.InstanceEncodingOption);
            List <CorrelationKey> keysToComplete  = CorrelationKey.BuildKeyList(saveWorkflowCommand.InstanceKeysToComplete);
            List <CorrelationKey> keysToFree      = CorrelationKey.BuildKeyList(saveWorkflowCommand.InstanceKeysToFree);

            ArraySegment <byte>[] dataProperties     = SerializationUtilities.SerializePropertyBag(saveWorkflowCommand.InstanceData, base.Store.InstanceEncodingOption);
            ArraySegment <byte>   metadataProperties = SerializationUtilities.SerializeMetadataPropertyBag(saveWorkflowCommand, base.InstancePersistenceContext, base.Store.InstanceEncodingOption);

            byte[] concatenatedKeyProperties = SerializationUtilities.CreateKeyBinaryBlob(keysToAssociate);
            bool   metadataConsistency       = (base.InstancePersistenceContext.InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None);
            bool   singleKeyToAssociate      = (keysToAssociate != null && keysToAssociate.Count == 1);

            parameters.Add(new SqlParameter {
                ParameterName = "@keysToAssociate", SqlDbType = SqlDbType.Xml, Value = singleKeyToAssociate ? DBNull.Value : SerializationUtilities.CreateCorrelationKeyXmlBlob(keysToAssociate)
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@singleKeyId", SqlDbType = SqlDbType.UniqueIdentifier, Value = singleKeyToAssociate ? keysToAssociate[0].KeyId : (object)DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@keysToComplete", SqlDbType = SqlDbType.Xml, Value = SerializationUtilities.CreateCorrelationKeyXmlBlob(keysToComplete)
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@keysToFree", SqlDbType = SqlDbType.Xml, Value = SerializationUtilities.CreateCorrelationKeyXmlBlob(keysToFree)
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@concatenatedKeyProperties", SqlDbType = SqlDbType.VarBinary, Size = -1, Value = (object)concatenatedKeyProperties ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@metadataIsConsistent", SqlDbType = SqlDbType.Bit, Value = metadataConsistency
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@encodingOption", SqlDbType = SqlDbType.TinyInt, Value = base.Store.InstanceEncodingOption
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@lastMachineRunOn", SqlDbType = SqlDbType.NVarChar, Size = 450, Value = SqlWorkflowInstanceStoreConstants.MachineName
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@executionStatus", SqlDbType = SqlDbType.NVarChar, Size = 450, Value = GetExecutionStatus(saveWorkflowCommand) ?? (object)DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@blockingBookmarks", SqlDbType = SqlDbType.NVarChar, Size = -1, Value = GetBlockingBookmarks(saveWorkflowCommand) ?? (object)DBNull.Value
            });

            ArraySegment <byte>[] properties = { dataProperties[0], dataProperties[1], dataProperties[2], dataProperties[3], metadataProperties };
            string[] dataPropertyParameters  = { "@primitiveDataProperties", "@complexDataProperties", "@writeOnlyPrimitiveDataProperties", @"writeOnlyComplexDataProperties", "@metadataProperties" };

            for (int i = 0; i < 5; i++)
            {
                SaveWorkflowAsyncResult.AddSerializedProperty(properties[i], parameters, dataPropertyParameters[i]);
            }

            this.SerializePromotedProperties(parameters, commandTextBuilder, saveWorkflowCommand);
        }