【问题标题】:How can I read a workflow instance property promoted as binary?如何读取提升为二进制的工作流实例属性?
【发布时间】:2014-03-21 15:31:59
【问题描述】:

就我而言,我提升了 UInt64 值。 UInt64 不能作为变体提升,所以我使用了promoteAsBinary。

这是我必须阅读的代码:

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);
}

我尝试将 byte[] 传递给 MemoryStream,然后使用带有默认编码、ASCI 和 UTF-8 的 BinaryReader,但它不起作用。我得到一个垃圾值。我将实例编码设置为无。

storeBehavior.InstanceEncodingOption = InstanceEncodingOption.None;

【问题讨论】:

    标签: c# .net workflow-foundation-4 workflowservice


    【解决方案1】:

    我遇到了同样的问题。

    我做了以下方法。您可以使用 gzip 保存工作流数据。 如果您使用它,则将压缩设置为 true。 泛型是数据被去灭菌的类型。

    顺便说一句:对于嵌套 using 结构感到抱歉。你应该避免这种类型的结构 这是处置的不好做法。见: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;
                }
            }
        }
    

    【讨论】:

    • 谢谢!我会试试这个
    猜你喜欢
    • 2015-05-19
    • 1970-01-01
    • 2014-05-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多