I am trying to add a new enum value for a specific protobuf serial class class in a new version of the application, and when testing, I noticed that the previous version throws an exception, given this new file format:
An unhandled exception of type 'ProtoBuf.ProtoException' occurred in protobuf-net.dll
Additional information: No {enum-type-name} enum is mapped to the wire-value 3
It is pretty obvious that he tells me that there is no enum value for the int 3 value, but I always thought that the protocol buffers default to the null value ("default") of the enumeration (if one exists), in case the actual enumeration value is not can be matched.
To clarify, this can be reproduced in the following example (I deliberately take the step of deserializing to another class to mimic an old application trying to load a new format):
// --- version 1 --- public enum EnumV1 { Default = 0, One = 1, Two = 2 } [ProtoContract] public class ClassV1 { [ProtoMember(1)] public EnumV1 Value { get; set; } } // --- version 2 --- public enum EnumV2 { Default = 0, One = 1, Two = 2, Three = 3 // <- newly added } [ProtoContract] public class ClassV2 { [ProtoMember(1)] public EnumV2 Value { get; set; } }
And the following code will not be executed:
// serialize v2 using the new app var v2 = new ClassV2() { Value = EnumV2.Three }; var v2data = Serialize(v2); // try to deserialize this inside the old app to v1 var v1roundtrip = Deserialize<ClassV1>(v2data);
Since v1 is in the open state, is there some metadata that I can use when serializing in v2 to avoid this problem? Of course, I can get rid of this problem by rewriting v2 to use a separate property and leave the enumeration values unmodified, but I would like to make the transitions backward compatible, if possible.