I serialize some configuration objects and store the result bytes within a database.
new BinaryFormatter().Serialize(memoryStream, instance);
Convert.ToBase64String(memoryStream.ToArray());
These objects will be deserialized later.
new BinaryFormatter().Deserialize(memoryStream);
It’s possible, that the Application has some new assembly versions at the time of deserialization. In general it works well, but sometimes I get a file load exception:
“The located assembly’s manifest definition does not match the assembly reference.”. The assemblies work all with strong naming, can that be the problem and how could I avoid this problem?
Thanks for help
Absolutely, using
BinaryFormatterwith database (i.e. long-term) storage is a bad idea;BinaryFormatterhastwothree big faults (by default):My blog post here raises two specific issues with this – obfuscation and automatically implemented properties… I won’t repeat the text here, but you may find it interesting.
I recommend the use of a contract based serialization.
XmlSerializerorDataContractSerializerwould suffice normally. If you want small efficient binary, then protobuf-net might be of interest. UnlikeBinaryFormatter, the binary from this is portable between implementations, extensible (for new fields), etc. And it is quicker and smaller, too.