Ershad Nozari
Ershad Nozari

Reputation: 667

.NET Framework to .NET Core migration serialization issue

I have ported a number of class libraries from .NET Framework 4.5 to .NET Standard 2.0. Using these libraries from a .NET Framework 4.8 console application works fine. However referencing the libraries from a .NET Core 2.2 console app, results in the following exception:

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

with stack trace

   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)

My inner most calling code to the framework that's failing is this:

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

Any ideas?

Upvotes: 0

Views: 1005

Answers (1)

Ershad Nozari
Ershad Nozari

Reputation: 667

Answer from Microsoft: “In .NET Core fewer types are binary serializable than in .NET Framework. This is by design because serialization based on BinaryFormatter has historically been fragile and prone to security problems. SyncHashtable is not currently serializable. However we should probably make it so. Meantime you need to avoid serializing it. This may mean using Hashtable and manually locking instead of using SyncHashtable. Or possibly (advanced) using the extension mechanisms of BinaryFormatter to work around the problem.”

Conversation on GitHub here: https://github.com/dotnet/corefx/issues/37708#issuecomment-493641762

Upvotes: 1

Related Questions