Assembly Independent Serialization in .NET
Asked Answered
A

3

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?

Augusto answered 22/8, 2011 at 21:51 Comment(2)
can you move the class to be deserialized into a separate assembly? apparently it isn't changing?Inviting
yes, I can. I'm looking for a way to skip the assembly version checking, and keep it on my own risk.Augusto
B
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.

Briton answered 22/8, 2011 at 23:7 Comment(3)
Does it make the deserialization assembly independent?Augusto
@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.Briton
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!Dichromate
A
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).

Apostil answered 22/8, 2011 at 21:57 Comment(6)
@Tigran I can talk about that one all day long ;pApostil
I can only make one +1 :P But seriously will have a look on it, cause I was need some serialization times ago... thanks !Tatar
+1 for protobuf-net. Any long-term storage of native CLR-serialized types is a disaster in the making.Vermont
Marc, which version of the Protobuf do you recommend?Augusto
@Kirk I would recommend protobuf-net, but that is because I've spent much of my spare time writing it ;p It is actually what we use here on stackoverflow, for exampleApostil
@Marc. What about Android? 1.0 2 or 3? Supported?Augusto
K
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)

Kendry answered 22/8, 2011 at 21:53 Comment(1)
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.Augusto

© 2022 - 2024 — McMap. All rights reserved.