Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何读取升级为二进制的工作流实例属性?_C#_.net_Workflow Foundation 4_Workflowservice - Fatal编程技术网

C# 如何读取升级为二进制的工作流实例属性?

C# 如何读取升级为二进制的工作流实例属性?,c#,.net,workflow-foundation-4,workflowservice,C#,.net,Workflow Foundation 4,Workflowservice,在我的例子中,我提升了UInt64值。UInt64不能升级为变体,所以我使用了promoteAsBinary 这是我必须阅读的代码: String sql = @"Select Value33, InstanceId from [System.Activities.DurableInstancing].[InstancePromotedProperties] where PromotionName = 'MyUInt64'"; try { string connectionSt

在我的例子中,我提升了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);
}
我尝试将字节[]传递给MemoryStream,然后使用带有默认编码、ASCI和UTF-8的BinaryReader,但没有成功。我得到一个垃圾值。我将实例enconding设置为none

storeBehavior.instanceEncoding选项=

InstanceEncoding选项。无

我遇到了与您相同的问题

我采用了以下方法。您可以使用gzip保存工作流数据。 如果使用此选项,请将compressed设置为true。 泛型是数据被取消消毒的类型

顺便说一句:很抱歉使用嵌套结构。您应该避免这种类型的构造 这是处理垃圾的坏习惯。见:

public T反序列化二进制数据(字节[]序列化二进制数据)
{
使用(var memoryStream=new memoryStream())
{
memoryStream.Write(serializedBinaryData,0,serializedBinaryData.Length);
memoryStream.Position=0;
如果(压缩)
{
使用(var gZipStream=new gZipStream(memoryStream,CompressionMode.Decompress))
{
使用(
var dictionaryReader=XmlDictionaryReader.CreateBinaryReader(gZipStream,
XmlDictionaryReaderQuotas
(.Max))
{
var netDataContractSerializer=新的netDataContractSerializer();
var deserializedData=netDataContractSerializer.ReadObject(dictionaryReader);
返回(T)反序列化数据;
}
}
}
使用(
var dictionaryReader=XmlDictionaryReader.CreateBinaryReader(memoryStream,
XmlDictionaryReaderQuotas
(.Max))
{
var deserializedData=新的NetDataContractSerializer().ReadObject(dictionaryReader);
返回(T)反序列化数据;
}
}
}

谢谢!我会试试这个
    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;
            }
        }
    }