public static Dictionary <XName, InstanceValue> DeserializeMetadataPropertyBag(byte[] serializedMetadataProperties, InstanceEncodingOption instanceEncodingOption)
        {
            Dictionary <XName, InstanceValue> dictionary = new Dictionary <XName, InstanceValue>();

            if (serializedMetadataProperties != null)
            {
                foreach (KeyValuePair <XName, object> pair in ObjectSerializerFactory.GetObjectSerializer(instanceEncodingOption).DeserializePropertyBag(serializedMetadataProperties))
                {
                    dictionary.Add(pair.Key, new InstanceValue(pair.Value));
                }
            }
            return(dictionary);
        }
        public static ArraySegment <byte>[] SerializePropertyBag(IDictionary <XName, InstanceValue> properties, InstanceEncodingOption encodingOption)
        {
            ArraySegment <byte>[] dataArrays = new ArraySegment <byte> [4];

            if (properties.Count > 0)
            {
                IObjectSerializer            serializer                   = ObjectSerializerFactory.GetObjectSerializer(encodingOption);
                XmlPropertyBag               primitiveProperties          = new XmlPropertyBag();
                XmlPropertyBag               primitiveWriteOnlyProperties = new XmlPropertyBag();
                Dictionary <XName, object>   complexProperties            = new Dictionary <XName, object>();
                Dictionary <XName, object>   complexWriteOnlyProperties   = new Dictionary <XName, object>();
                Dictionary <XName, object>[] propertyBags                 = new Dictionary <XName, object>[] { primitiveProperties, complexProperties,
                                                                                                               primitiveWriteOnlyProperties, complexWriteOnlyProperties };

                foreach (KeyValuePair <XName, InstanceValue> property in properties)
                {
                    bool isComplex   = (XmlPropertyBag.GetPrimitiveType(property.Value.Value) == PrimitiveType.Unavailable);
                    bool isWriteOnly = (property.Value.Options & InstanceValueOptions.WriteOnly) == InstanceValueOptions.WriteOnly;
                    int  index       = (isWriteOnly ? 2 : 0) + (isComplex ? 1 : 0);
                    propertyBags[index].Add(property.Key, property.Value.Value);
                }

                // Remove the properties that are already stored as individual columns from the serialized blob
                primitiveWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.StatusPropertyName);
                primitiveWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.LastUpdatePropertyName);
                primitiveWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.PendingTimerExpirationPropertyName);

                complexWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.BinaryBlockingBookmarksPropertyName);

                for (int i = 0; i < propertyBags.Length; i++)
                {
                    if (propertyBags[i].Count > 0)
                    {
                        if (propertyBags[i] is XmlPropertyBag)
                        {
                            dataArrays[i] = serializer.SerializeValue(propertyBags[i]);
                        }
                        else
                        {
                            dataArrays[i] = serializer.SerializePropertyBag(propertyBags[i]);
                        }
                    }
                }
            }

            return(dataArrays);
        }
        public static ArraySegment <byte> SerializeKeyMetadata(IDictionary <XName, InstanceValue> metadataProperties, InstanceEncodingOption encodingOption)
        {
            if ((metadataProperties == null) || (metadataProperties.Count <= 0))
            {
                return(new ArraySegment <byte>());
            }
            Dictionary <XName, object> dictionary = new Dictionary <XName, object>();

            foreach (KeyValuePair <XName, InstanceValue> pair in metadataProperties)
            {
                if ((pair.Value.Options & InstanceValueOptions.WriteOnly) != InstanceValueOptions.WriteOnly)
                {
                    dictionary.Add(pair.Key, pair.Value.Value);
                }
            }
            return(ObjectSerializerFactory.GetObjectSerializer(encodingOption).SerializePropertyBag(dictionary));
        }
        public static Dictionary <XName, InstanceValue> DeserializeMetadataPropertyBag(byte[] serializedMetadataProperties, InstanceEncodingOption instanceEncodingOption)
        {
            Dictionary <XName, InstanceValue> metadataProperties = new Dictionary <XName, InstanceValue>();

            if (serializedMetadataProperties != null)
            {
                IObjectSerializer          serializer  = ObjectSerializerFactory.GetObjectSerializer(instanceEncodingOption);
                Dictionary <XName, object> propertyBag = serializer.DeserializePropertyBag(serializedMetadataProperties);

                foreach (KeyValuePair <XName, object> property in propertyBag)
                {
                    metadataProperties.Add(property.Key, new InstanceValue(property.Value));
                }
            }

            return(metadataProperties);
        }
Ejemplo n.º 5
0
 public LoadWorkflowAsyncResult
 (
     InstancePersistenceContext context,
     InstancePersistenceCommand command,
     SqlWorkflowInstanceStore store,
     SqlWorkflowInstanceStoreLock storeLock,
     Transaction currentTransaction,
     TimeSpan timeout,
     AsyncCallback callback,
     object state
 ) :
     base(context, command, store, storeLock, currentTransaction, timeout, callback, state)
 {
     this.associatedInstanceKeys = new Dictionary <Guid, IDictionary <XName, InstanceValue> >();
     this.completedInstanceKeys  = new Dictionary <Guid, IDictionary <XName, InstanceValue> >();
     this.objectSerializer       = ObjectSerializerFactory.GetDefaultObjectSerializer();
 }
        public static ArraySegment <byte> SerializeMetadataPropertyBag(SaveWorkflowCommand saveWorkflowCommand,
                                                                       InstancePersistenceContext context, InstanceEncodingOption instanceEncodingOption)
        {
            IObjectSerializer          serializer             = ObjectSerializerFactory.GetObjectSerializer(instanceEncodingOption);
            Dictionary <XName, object> propertyBagToSerialize = new Dictionary <XName, object>();

            if (context.InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None)
            {
                foreach (KeyValuePair <XName, InstanceValue> metadataProperty in context.InstanceView.InstanceMetadata)
                {
                    if ((metadataProperty.Value.Options & InstanceValueOptions.WriteOnly) == 0)
                    {
                        propertyBagToSerialize.Add(metadataProperty.Key, metadataProperty.Value.Value);
                    }
                }
            }

            foreach (KeyValuePair <XName, InstanceValue> metadataChange in saveWorkflowCommand.InstanceMetadataChanges)
            {
                if (metadataChange.Value.IsDeletedValue)
                {
                    if (context.InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None)
                    {
                        propertyBagToSerialize.Remove(metadataChange.Key);
                    }
                    else
                    {
                        propertyBagToSerialize[metadataChange.Key] = new DeletedMetadataValue();
                    }
                }
                else if ((metadataChange.Value.Options & InstanceValueOptions.WriteOnly) == 0)
                {
                    propertyBagToSerialize[metadataChange.Key] = metadataChange.Value.Value;
                }
            }

            if (propertyBagToSerialize.Count > 0)
            {
                return(serializer.SerializePropertyBag(propertyBagToSerialize));
            }

            return(new ArraySegment <byte>());
        }
        public static ArraySegment <byte> SerializeKeyMetadata(IDictionary <XName, InstanceValue> metadataProperties, InstanceEncodingOption encodingOption)
        {
            if (metadataProperties != null && metadataProperties.Count > 0)
            {
                Dictionary <XName, object> propertyBag = new Dictionary <XName, object>();

                foreach (KeyValuePair <XName, InstanceValue> property in metadataProperties)
                {
                    if ((property.Value.Options & InstanceValueOptions.WriteOnly) != InstanceValueOptions.WriteOnly)
                    {
                        propertyBag.Add(property.Key, property.Value.Value);
                    }
                }

                IObjectSerializer serializer = ObjectSerializerFactory.GetObjectSerializer(encodingOption);
                return(serializer.SerializePropertyBag(propertyBag));
            }

            return(new ArraySegment <byte>());
        }
        public static ArraySegment <byte> SerializeMetadataPropertyBag(SaveWorkflowCommand saveWorkflowCommand, InstancePersistenceContext context, InstanceEncodingOption instanceEncodingOption)
        {
            IObjectSerializer          objectSerializer = ObjectSerializerFactory.GetObjectSerializer(instanceEncodingOption);
            Dictionary <XName, object> dictionary       = new Dictionary <XName, object>();

            if (context.InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None)
            {
                foreach (KeyValuePair <XName, InstanceValue> pair in context.InstanceView.InstanceMetadata)
                {
                    if ((pair.Value.Options & InstanceValueOptions.WriteOnly) == InstanceValueOptions.None)
                    {
                        dictionary.Add(pair.Key, pair.Value.Value);
                    }
                }
            }
            foreach (KeyValuePair <XName, InstanceValue> pair2 in saveWorkflowCommand.InstanceMetadataChanges)
            {
                if (pair2.Value.IsDeletedValue)
                {
                    if (context.InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None)
                    {
                        dictionary.Remove(pair2.Key);
                    }
                    else
                    {
                        DeletedMetadataValue value2 = new DeletedMetadataValue();
                        dictionary[pair2.Key] = value2;
                    }
                }
                else if ((pair2.Value.Options & InstanceValueOptions.WriteOnly) == InstanceValueOptions.None)
                {
                    dictionary[pair2.Key] = pair2.Value.Value;
                }
            }
            if (dictionary.Count > 0)
            {
                return(objectSerializer.SerializePropertyBag(dictionary));
            }
            return(new ArraySegment <byte>());
        }
 public static ArraySegment <byte>[] SerializePropertyBag(IDictionary <XName, InstanceValue> properties, InstanceEncodingOption encodingOption)
 {
     ArraySegment <byte>[] segmentArray = new ArraySegment <byte> [4];
     if (properties.Count > 0)
     {
         IObjectSerializer            objectSerializer = ObjectSerializerFactory.GetObjectSerializer(encodingOption);
         XmlPropertyBag               bag             = new XmlPropertyBag();
         XmlPropertyBag               bag2            = new XmlPropertyBag();
         Dictionary <XName, object>   dictionary      = new Dictionary <XName, object>();
         Dictionary <XName, object>   dictionary2     = new Dictionary <XName, object>();
         Dictionary <XName, object>[] dictionaryArray = new Dictionary <XName, object>[] { bag, dictionary, bag2, dictionary2 };
         foreach (KeyValuePair <XName, InstanceValue> pair in properties)
         {
             bool flag  = XmlPropertyBag.GetPrimitiveType(pair.Value.Value) == PrimitiveType.Unavailable;
             int  index = (((pair.Value.Options & InstanceValueOptions.WriteOnly) == InstanceValueOptions.WriteOnly) ? 2 : 0) + (flag ? 1 : 0);
             dictionaryArray[index].Add(pair.Key, pair.Value.Value);
         }
         bag2.Remove(SqlWorkflowInstanceStoreConstants.StatusPropertyName);
         bag2.Remove(SqlWorkflowInstanceStoreConstants.LastUpdatePropertyName);
         bag2.Remove(SqlWorkflowInstanceStoreConstants.PendingTimerExpirationPropertyName);
         dictionary2.Remove(SqlWorkflowInstanceStoreConstants.BinaryBlockingBookmarksPropertyName);
         for (int i = 0; i < dictionaryArray.Length; i++)
         {
             if (dictionaryArray[i].Count > 0)
             {
                 if (dictionaryArray[i] is XmlPropertyBag)
                 {
                     segmentArray[i] = objectSerializer.SerializeValue(dictionaryArray[i]);
                 }
                 else
                 {
                     segmentArray[i] = objectSerializer.SerializePropertyBag(dictionaryArray[i]);
                 }
             }
         }
     }
     return(segmentArray);
 }
 public static object DeserializeObject(byte[] serializedObject, InstanceEncodingOption encodingOption)
 {
     return(ObjectSerializerFactory.GetObjectSerializer(encodingOption).DeserializeValue(serializedObject));
 }
 public static ArraySegment <byte> SerializeObject(object objectToSerialize, InstanceEncodingOption encodingOption)
 {
     return(ObjectSerializerFactory.GetObjectSerializer(encodingOption).SerializeValue(objectToSerialize));
 }
Ejemplo n.º 12
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++;
                }
            }
        }
        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++;
                }
            }
        }