Some gotchas in backward compatibility
suggest changeThis small example shows how you can lose backward compatibility in your programs if you do not take care of this in advance, and ways to get more control of the serialization process.
At first, we will write an example of the first version of the program:
Version 1
[Serializable]
class Data
{
[OptionalField]
private int _version;
public int Version
{
get { return _version; }
set { _version = value; }
}
}
And now, let us assume that the second version of the program added a new class, and we need to store it in a collection.
Now code will look like this:
Version 2
[Serializable]
class NewItem
{
[OptionalField]
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
[Serializable]
class Data
{
[OptionalField]
private int _version;
public int Version
{
get { return _version; }
set { _version = value; }
}
[OptionalField]
private List<NewItem> _newItems;
public List<NewItem> NewItems
{
get { return _newItems; }
set { _newItems = value; }
}
}
And code to serialize and deserialize
private static byte[] SerializeData(object obj)
{
var binaryFormatter = new BinaryFormatter();
using (var memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, obj);
return memoryStream.ToArray();
}
}
private static object DeserializeData(byte[] bytes)
{
var binaryFormatter = new BinaryFormatter();
using (var memoryStream = new MemoryStream(bytes))
return binaryFormatter.Deserialize(memoryStream);
}
And so, what happens when you serialize data in the v2 program and try to deserialize it in the v1 program?
You get an exception:
System.Runtime.Serialization.SerializationException was unhandled
Message=The ObjectManager found an invalid number of fixups. This usually indicates a problem in the Formatter.Source=mscorlib
StackTrace:
at System.Runtime.Serialization.ObjectManager.DoFixups()
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)
at Microsoft.Samples.TestV1.Main(String[] args) in c:\Users\andrew\Documents\Visual Studio 2013\Projects\vts\CS\V1 Application\TestV1Part2\TestV1Part2.cs:line 29
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Why?
The ObjectManager has different logic to resolve dependencies for arrays and for reference and value types. The field added here is a List<NewItem>. BinaryFormatter records that collection, and the element type is absent from the v1 assembly.
When ObjectManager attempts to resolve dependencies it builds a graph. When it sees the collection, it cannot fix it immediately, so it creates a dummy reference and then fixes the collection later.
Since this type is not in the assembly, the dependencies can’t be fixed. For some reason, it does not remove the collection from the list of elements for the fixes and at the end it throws “IncorrectNumberOfFixups”.
These are some gotchas in the process of serialization. For some reason, the failure shows up for collections of new reference types.
A Note:
Similar code will work correctly if you do not use collections of new classes
And the first way to fix it and maintain compatibility?
- Use a collection of new structures rather than classes, or use a dictionary (classes are possible), because a dictionary is a collection of
KeyValuePair(a structure) - Use ISerializable, if you can’t change the old code