Reputation: 170
After updating to the latest protobuf-net (2.0.0.601) I now get an exception when trying to serialize a value of type System.Version.
[ProtoContract(SkipConstructor=true)]
public class ServerLoginInfo
{
[ProtoMember(1)]
public Version ServerVersion { get; set; }
}
This used to work fine in 2.0.0.480 without doing anything special.
Is it possible to get it working in the new version or is my only option to rollback to the old version?
(I also need the serialization/deserialization to be backwards compatible with the old protobuf-net version.)
Upvotes: 6
Views: 1294
Reputation: 1646
I would recommend using a surrogate for Version instead, since enabling the AllowParseableTypes option can have the side effect of disabling some configuration for other types that apply to AllowParseableTypes.
More detail here: https://github.com/mgravell/protobuf-net/issues/183
To do it this way, add the following initializer:
RuntimeTypeModel.Default.Add(typeof(Version), false).SetSurrogate(typeof(VersionSurrogate));
and the following type:
[ProtoContract]
public class VersionSurrogate
{
[ProtoMember(1)]
public string Version { get; set; }
public static implicit operator VersionSurrogate(Version value)
{
return new VersionSurrogate
{
Version = value.ToString()
};
}
public static implicit operator Version(VersionSurrogate value)
{
return System.Version.Parse(value.Version);
}
}
Upvotes: 2
Reputation: 1062780
I suspect this relates to AllowParseableTypes - i.e. whether it looks for a static Parse method as a fallback. If i remember, this can't be enabled on the default model, but I think I'll remove that restriction in the next deploy. But for now:
var model = TypeModel.Create();
model.AllowParseableTypes = true;
Then store model (and reuse it), and use model.Serialize/model.Deserialize.
If I change this default-model restriction, then just:
RuntimeTypeModel.Default.AllowParseableTypes = true;
Upvotes: 4