Version invariant deserialization in .NET

If you have serialized an object using a previous version of a library or program, after you try to deserialize this object again you might encounter the following exception:

{“Exception has been thrown by the target of an invocation.”}

The inner exception might read:

[System.IO.FileLoadException] = {“Could not load file or assembly ‘Accord.Math, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)”:”Accord.Math, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7″}

In this case, it is very likely that those exceptions are occurring because the .NET run-time is looking for an assembly with the specific version indicated above. Even if you have new assemblies with exact the same name and exact public key token, the .NET might still refuse to deserialize it.

In order to get around this, put the following static class into your application:

Now, go back where you were using your deserializer and getting that exception, and instead of calling formatter.Deserialize, call formatter.DeserializeAnyVersion:

 

Deserialization now might work as expected; but please keep in mind that we might be loosing some security here. However, this might be a concern only if your application is dynamically loading assemblies at run-time.

Here are some resources discussing the problem:

Such extension method will also be included in the Accord.NET Framework.

Leave a Reply

Your email address will not be published. Required fields are marked *