8

I use Serialization/DeSerialization Technique. BinaryFormatter class. Each time when new assembly is created the BinaryFormatter can't Deserialize binary data even if the class structure is the same, but the Assembly version differs. Is it possible to Deserialize binary buffer with no checking the assembly version if the class structure stays unchanged?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • can you move the class to be deserialized into a separate assembly? apparently it isn't changing? – mtijn Aug 22 '11 at 21:54
  • yes, I can. I'm looking for a way to skip the assembly version checking, and keep it on my own risk. –  Aug 22 '11 at 21:58

3 Answers3

8

Try this:

public sealed class CurrentAssemblyDeserializationBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        return Type.GetType(String.Format("{0}, {1}", typeName, Assembly.GetExecutingAssembly().FullName));
    }
}
formatter.Binder = new CurrentAssemblyDeserializationBinder();
formatter.Deserialize(inStream);

Thread Poster Added:

Yes, it works. Just make sure if any types of System.Generic or other Libs in the binary data are present, then you have to pass them through without changes. "ResizableControls" - old Assembly lib' name, "EntityLib" - new Assembly name. Also, the Version number is to be replaced as well, on demand.

public sealed class CurrentAssemblyDeserializationBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        string name;
        if (assemblyName.Contains("ResizableControl"))
        {
            name = Assembly.GetAssembly(typeof(EntityLib.Pattern)).ToString();
        }
        else
        {
            name = assemblyName;
        }
        return Type.GetType(String.Format("{0}, {1}",
            typeName.Replace("ResizableControl", "EntityLib"), name));
    }
}

Thanks, it is exactly what I needed.

Sebastian
  • 6,293
  • 6
  • 34
  • 47
Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
  • Does it make the deserialization assembly independent? –  Aug 22 '11 at 23:25
  • @Kirk Robb The binary formatter stores the type information within the serialized object so that it knows what type to deserialize it to. By inheriting from SerializationBinder class it is possible to redirect all the requests for types from the binary formatter to the types containing in the current assembly. Let me know if it works. – Jacob Seleznev Aug 22 '11 at 23:52
  • Maybe using another serialization technique is better, like Marc Gravell said. However you just saved my life with this. I'm using it in a binary socket connection which is really nice to do with the BinaryFormatter. Thank you! – Vinz Dec 11 '15 at 17:10
6

That is inherent with BinaryFormatter. There are some advanced things you can do to get around it (with surrogates etc), but it is not easy and I honestly don't recommend it.

I strongly suggest you look at a contract-based serializer; for example:

  • XmlSerializer
  • DataContractSerializer (but not NetDataContractSerializer)
  • protobuf-net

(I'm biased towards the last, as it gives a much more efficient output, and deliberately avoids a few more versioning issues)

In all those cases, the data storage does not (at least, with the default settings) include any knowledge of the types, except the contract as implied by the names, or as specified (typically in attributes).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

I think that is a know problem with BinaryFormatter - here is a possible solution you can controll which type to load with a SerializationBinder - the link provides code and an example how to use this (in almost all .net languagues)

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • Thanks for the reply. That's cool. There is one "but"- in my case, the class structure is the same, the assembly is different, even Version of the Assembly is the same, but BinaryFormatter check all details, suchas File Create Date. And it makes the Deserialization undoable. –  Aug 22 '11 at 23:03