[英]How do I deserialize binary data into an object in .net Core, when the initial object was serialized with Full .net Framework?
I have an application which stores some data in a SQL database in a binary field. 我有一个应用程序,该应用程序将一些数据存储在二进制字段的SQL数据库中。 These data are serialized from a .net object in traditional .NET application with binary serialization . 这些数据是使用二进制序列化从传统.NET应用程序中的.net对象进行序列化的 。 I'm writing a .net core application that needs to interface with the above but reading the binary data and making sense of them. 我正在编写一个.net核心应用程序,该应用程序需要与上述接口进行交互,但需要读取二进制数据并使它们有意义。
Ideally I want to be able just to deserialize these data with .net Core as if I was running full .net framework. 理想情况下,我希望能够使用.net Core反序列化这些数据,就像运行完整的.net框架一样。 The data itself are not complex, they are just a Dictionary<string,string>
produced by the old asp.net profile provider . 数据本身并不复杂,它们只是由旧的asp.net配置文件提供程序生成的Dictionary<string,string>
。
I need to both read and write the binary data from .net core code. 我需要从.net核心代码读取和写入二进制数据。
I understand that support for BinaryFormatter is coming to .net core. 我理解的BinaryFormatter的支持即将于.NET的核心。 In the meanwhile, is there anything not very complicated I can do right now to serialize / deserialize data with .net core as full .net framework would? 在此期间,有什么不是很复杂我现在序列化与.NET的核心/反序列化数据为完整的.NET框架将能做到?
Looks like we have to write our own BinaryReader/BinaryWriter methods... 看来我们必须编写我们自己的BinaryReader / BinaryWriter方法...
SCHEMA 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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.