Stability of .NET serialization across different framework versions

狂风中的少年 提交于 2019-11-29 10:02:37

The chances are low (but not zero!) that there will be changes between framework versions. The intention would be that you should be able to use binary serialization and remoting to communicate between a client and a server running different framework versions. The incompatibility between .NET 1.x and 2.0 is a bug for which a patch is available.

However binary serialization has other issues, especially poor support for versioning of the structure you're serializing. From the use case you've described, Xml serialization is the obvious choice: DataContractSerializer being more flexible than XmlSerializer if you don't mind the dependency on .NET 3.x.

You can't guarantee that the .NET framework 2.0 will always be installed on future versions of Windows. But I'm sure Microsoft will work hard to ensure that most .NET 2.0 apps will run unchanged on .NET 4.x and later versions. I don't have any references for this: any such commitment would in any case only really apply to the next version of Windows (Windows 7).

The rule of thumb is generally: XML serialization should be able to survive new framework versions and therefore can be stored long-term, but binary serialization cannot (and therefore should only ever be transient).

What serializer are you using? In many ways, a serializer like XmlSerializer or DataContractSerializer buffers you from many details, and provides simpler extensibility options. At some point, a new CLR version will undoubtably be necessary - so I don't think anybody can make any guarantees about 2.0.50727; you should be safe short-term, though. And I would hope for fewer breaking changes...

[updated following note on another reply]

If you want a binary format for space/performance reasons, then another option is to use a different binary serializer. For example, protobuf-net works on all .NET variants*, but the binary format (dvised by Google) is cross-platform compatible (Java, C++, etc) - making it very portable, quick, and small.

*=I haven't tried it on micro framework, but CF, Silverlight, Mono, .NET 2.0 etc are all supported.

If compatibility is a concern, the ISerializable interface might be the cure you're looking for. This interface gives you more control over how items are serialized. For more info try this article on msdn.

I have two things to add to the other answers...

First, making use of a custom SerializationBinder can get you round lots of difficulties importing legacy serialized data.

Second, I consider it mandatory to write extensive unit tests for any persisted data. I always do two tests in particular:

  1. Round-trip test - can you serialize and deserialize your objects and get exactly the same thing back?
  2. Legacy import test - make sure you have versions of serialized data exported from every released version of your app. Import the data and check that everything comes back in as expected.

You do not have to use XML in order to gain higher flexibility and versioning.

I have used Simon Hewitt's open source library, see Optimizing Serialization in .NET - part 2 instead of default .NET serialisation. It offers some automation, but essentially you can control the stream of information that is serialised and deserialised. For versioning the (file) version can be serialised first and at deserialisation time the way the stream of information is interpreted is dependent on the version.

It is quite simple to do, although somewhat tedious due to the explicit serialisation / deserialisation.

As a bonus it is 20-40 times faster and takes up less space for large data sets (but may be unimportant in your case).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!