【问题标题】:.NET Framework to .NET Core migration serialization issue.NET Framework 到 .NET Core 迁移序列化问题
【发布时间】:2019-05-16 22:48:06
【问题描述】:

我已将许多类库从 .NET Framework 4.5 移植到 .NET Standard 2.0。从 .NET Framework 4.8 控制台应用程序中使用这些库可以正常工作。但是,从 .NET Core 2.2 控制台应用程序引用库会导致以下异常:

SerializationException: Type 'System.Collections.Hashtable+SyncHashtable' in Assembly 'System.Runtime.Extensions, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable

带有堆栈跟踪

   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.CheckSerializable(Type t)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObject(ParseRecord pr)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr)
   at System.Runtime.Serialization.Formatters.Binary.BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
   at System.Runtime.Serialization.Formatters.Binary.BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(BinaryParser serParser, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, Boolean check)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)

我对失败的框架的最内部调用代码是这样的:

public static object Deserialize(BinaryReader binaryReader)
{
    BinaryReader binaryReader = new BinaryReader(inStream);
    BinaryFormatter binaryFormatter = new BinaryFormatter();
    return binaryFormatter.Deserialize(binaryReader.BaseStream);
}

有什么想法吗?

【问题讨论】:

标签: .net .net-core .net-standard-2.0


【解决方案1】:

来自 Microsoft 的回答:“在 .NET Core 中,可二进制序列化的类型比在 .NET Framework 中要少。这是设计使然,因为基于 BinaryFormatter 的序列化历来是脆弱的并且容易出现安全问题。 SyncHashtable 当前不可序列化。但是,我们可能应该这样做。同时,您需要避免对其进行序列化。这可能意味着使用 Hashtable 并手动锁定而不是使用 SyncHashtable。或者可能(高级)使用 BinaryFormatter 的扩展机制来解决这个问题。”

在 GitHub 上的对话:https://github.com/dotnet/corefx/issues/37708#issuecomment-493641762

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2021-04-07
    • 1970-01-01
    • 2020-05-28
    • 2022-07-15
    • 1970-01-01
    相关资源
    最近更新 更多