I'm migrating a set of applications from .Net Framework 4.7 to .Net 5.0, but I'm running into some issues with deserializion.
The example below illustrates the problem I'm facing. If both applications are .Net framework, it works fine. If both are .Net 5.0, it works fine. But if one application is .Net 4.7 and the other is 5.0, I get an exception when deserializing the type List<IList>
.
.Net 5.0 application CreateData:
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Test;
namespace CreateData
{
class Program
{
static void Main()
{
DataStructure dataStructure = new DataStructure();
ArrayList arrayList = new ArrayList();
arrayList.Add(42);
dataStructure.data.Add(arrayList);
// Open a stream for writing
FileStream fs = new FileStream(@"C:\DataFile.dat", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, dataStructure);
fs.Close();
Console.WriteLine("Data file created, press Enter to exit");
Console.ReadLine();
}
}
}
.Net Standard 2.0 library Lib:
using System;
using System.Collections;
using System.Collections.Generic;
namespace Test
{
[Serializable]
public class DataStructure
{
public DataStructure()
{
data = new List<IList>();
}
public List<IList> data;
}
}
.Net 4.7 application ReadData:
using System;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Test;
namespace ReadData
{
class Program
{
static void Main()
{
DataStructure dataStructure = new DataStructure();
// Open the data file
FileStream fs = new FileStream(@"C:\DataFile.dat",FileMode.Open);
// Construct the binary formatter
BinaryFormatter bf = new BinaryFormatter();
// deserialize
dataStructure = (DataStructure) bf.Deserialize(fs);
fs.Close();
// Announce success
Console.WriteLine("Value = {0}", dataStructure.data.First()[0]);
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
}
System.Runtime.Serialization.SerializationException: Unable to load type System.Collections.Generic.List`1[[System.Collections.IList, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] required for deserialization. at System.Runtime.Serialization.ObjectManager.CompleteObject(ObjectHolder holder, Boolean bObjectFullyComplete) at System.Runtime.Serialization.ObjectManager.DoNewlyRegisteredObjectFixups(ObjectHolder holder) at System.Runtime.Serialization.ObjectManager.RegisterObject(Object obj, Int64 objectID, SerializationInfo info, Int64 idOfContainingObj, MemberInfo member, Int32[] arrayIndex) at System.Runtime.Serialization.Formatters.Binary.ObjectReader.RegisterObject(Object obj, ParseRecord pr, ParseRecord objectPr, Boolean bIsString) at System.Runtime.Serialization.Formatters.Binary.ObjectReader.RegisterObject(Object obj, ParseRecord pr, ParseRecord objectPr) at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObjectEnd(ParseRecord pr) at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr) at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream) at ReadData.Program.Main() in
Is there any way to load the type System.Collections.Generic.List1[[System.Collections.IList, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
? What can I do to resolve this?
If at all possible, I would like to migrate one application at a time instead of migrating the entire code base in one go.
Thanks!