当使用Full .net Framework序列化初始对象时,如何将二进制数据反序列化为.net Core中的对象?

时间:2017-02-20 22:54:31

标签: c# .net serialization .net-core

我有一个应用程序,它将一些数据存储在二进制字段的SQL数据库中。这些数据是使用binary serialization从传统.NET应用程序中的.net对象序列化的。我正在编写一个.net核心应用程序,它需要与上面的内容进行交互,但是要读取二进制数据并理解它们。

理想情况下,我希望能够使用.net Core反序列化这些数据,就像我运行完整的.net框架一样。数据本身并不复杂,只是旧版asp.net profile provider生成的Dictionary<string,string>

我需要从.net核心代码读取和写入二进制数据。

我理解支持BinaryFormatter is coming到.net核心。与此同时,有什么不是很复杂我可以做现在使用.net核心序列化/反序列化数据作为完整的.net框架会吗?

1 个答案:

答案 0 :(得分:0)

看起来我们必须编写自己的BinaryReader / BinaryWriter方法......

<强> SCHEMA

CREATE TABLE dbo.BinaryTest (
    BinaryData varbinary(max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

<强> CODE

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;

namespace StackOverflow.ConsoleTester
{
    public class Program
    {
        private const String SqlConnectionString = @"Server={Server};Database={Database};User={User};Password={Password};";

        public static void Main(String[] args)
        {
            using (var db = new SqlConnection(SqlConnectionString))
            {
                var sqlCommand = new SqlCommand("DELETE FROM dbo.BinaryTest;", db) { CommandType = CommandType.Text };

                db.Open();

                sqlCommand.ExecuteNonQuery();
            }

            using (var db = new SqlConnection(SqlConnectionString))
            {
                var serializedData = new Dictionary<String, String> {{"key1", "value1"}, {"key2", "value2"}};

                var binaryData = WriteBinaryData(serializedData);

                var sqlCommand = new SqlCommand("INSERT INTO dbo.BinaryTest (BinaryData) VALUES (@BinaryData);", db) { CommandType = CommandType.Text };

                var parameter = new SqlParameter("BinaryData", SqlDbType.VarBinary) {Value = binaryData};

                sqlCommand.Parameters.Add(parameter);

                db.Open();

                sqlCommand.ExecuteNonQuery();
            }

            Dictionary<String, String> deserializedData = null;

            using (var db = new SqlConnection(SqlConnectionString))
            {
                var sqlCommand = new SqlCommand("SELECT BinaryData FROM dbo.BinaryTest", db) { CommandType = CommandType.Text };

                db.Open();

                var reader = sqlCommand.ExecuteReader();

                while (reader.Read())
                {
                    deserializedData = ReadBinaryData(reader.GetSqlBinary(0));
                }
            }

            if (deserializedData != null)
            {
                foreach (var item in deserializedData)
                {
                    Console.WriteLine($"Key: {item.Key}; Value: {item.Value}");
                }
            }

            Console.ReadKey();
        }

        private static Byte[] WriteBinaryData(Dictionary<String, String> data)
        {
            var memoryStream = new MemoryStream();
            var binaryWriter = new BinaryWriter(memoryStream);

            foreach (var item in data)
            {
                binaryWriter.Write(item.Key);
                binaryWriter.Write(item.Value);
            }

            var binaryData = memoryStream.ToArray();

            return binaryData;
        }

        private static Dictionary<String, String> ReadBinaryData(SqlBinary data)
        {
            var model = new Dictionary<String, String>();

            var memoryStream = new MemoryStream(data.Value);
            var binaryReader = new BinaryReader(memoryStream);

            while (binaryReader.BaseStream.Position != binaryReader.BaseStream.Length)
            {
                model.Add(binaryReader.ReadString(), binaryReader.ReadString());
            }

            return model;
        }
    }
}