简体   繁体   中英

How can I read a workflow instance property promoted as binary?

In my case, I have promoted a UInt64 value. UInt64's cannot be promoted as variant, so I used promoteAsBinary.

This is the code I have to read it:

String sql =
@"Select Value33, InstanceId from
  [System.Activities.DurableInstancing].[InstancePromotedProperties]
  where PromotionName = 'MyUInt64'";

try
{
    string connectionString = ConfigurationManager.ConnectionStrings
        ["InstanceStore"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            Console.WriteLine("Promoted values:");
            while (reader.Read())
            {
                 byte[] result = (byte[])reader["Value33"];

                 // How do I turn this byte[] into an UInt64??
            }
        }
    }
}
catch (Exception exception)
{
    Console.WriteLine("Query Unhandled exception: {0}",
        exception.Message);
}

I tried passing the byte[] to a MemoryStream and then using BinaryReader with the Default enconding, ASCI, and UTF-8 and it didn't work. I get a garbage value. I set the instance enconding to none.

storeBehavior.InstanceEncodingOption = InstanceEncodingOption.None;

I ran into the same problem you have.

I made the following method. you can use gzip for saving workflow data. If you use this than set compressed to true. The generic is the type to which the data is desterilized.

BTW: sorry for the nested using structure. You should avoid this type of construction it is a bad practice for disposing. See: http://msdn.microsoft.com/en-us/library/ms182334.aspx

    public T DeserializeBinaryData<T>(byte[] serializedBinaryData)
    {
        using (var memoryStream = new MemoryStream())
        {
            memoryStream.Write(serializedBinaryData, 0, serializedBinaryData.Length);
            memoryStream.Position = 0;

            if (compressed)
            {
                using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    using (
                        var dictionaryReader = XmlDictionaryReader.CreateBinaryReader(gZipStream,
                                                                                      XmlDictionaryReaderQuotas
                                                                                          .Max))
                    {
                        var netDataContractSerializer = new NetDataContractSerializer();
                        var deserializedData = netDataContractSerializer.ReadObject(dictionaryReader);
                        return (T)deserializedData;
                    }
                }
            }

            using (
                var dictionaryReader = XmlDictionaryReader.CreateBinaryReader(memoryStream,
                                                                              XmlDictionaryReaderQuotas
                                                                                  .Max))
            {
                var deserializedData = new NetDataContractSerializer().ReadObject(dictionaryReader);
                return (T)deserializedData;
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM