I'm using NetDataContractSerializer to exchange data among applications. I would like the ReadObject method throw an exception when assembly versions don't match.

Now, for example, when I serialized my object from assembly version 1.0.0.0 and later deserialize it having same assembly but now version 1.0.0.1, the NetDataContractSerializer happily swallows the stream and deserializes without problems.

Is it possible to abort the deserialization process when versions don't match?

UPDATE: I need version intolerance because of many reasons. This is both a requirement from a customer, as well as a requirement to be absolutely certain that import processes the file that was exported from the same version of the application. In case of my application, changing version makes useless the previous import process, because version change equals to more or less involved internal structure change.

有帮助吗?

解决方案

Ugly but effective (ideally on your root object):

[DataMember]
private string AppVersion {
    get { return CurrentAppVersion; }
    set {
        if(value != CurrentAppVersion) throw new InvalidOperationException(
            "Data from version " + value + " is not compatible");
    }
}
private const string CurrentAppVersion = "1.0.11a";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top