5

I am using protobuf-net to serialize an object and I get exception:

Cannot apply changes to property TestProject.TestMessage.ClientId

with stacktrace:

at ProtoBuf.Serializers.PropertyDecorator.SanityCheck(TypeModel model, PropertyInfo property, IProtoSerializer tail, Boolean& writeValue, Boolean nonPublic, Boolean allowInternal)
at ProtoBuf.Serializers.PropertyDecorator..ctor(TypeModel model, Type forType, PropertyInfo property, IProtoSerializer tail)
at ProtoBuf.Meta.ValueMember.BuildSerializer()
at ProtoBuf.Meta.ValueMember.get_Serializer()
at ProtoBuf.Meta.MetaType.BuildSerializer()
at ProtoBuf.Meta.MetaType.get_Serializer()
at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest)
at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value)
at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context)
at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value)
at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance)

My class is the following:

[DataContract]
public class TestMessage
{
    private int clientId;
    [DataMember(Order = 1)]
    public int ClientId
    {
        get { return clientId; }
    }

    private string name;
    [DataMember(Order = 2)]
    public string Name
    {
        get { return name; }
    }

    public TestMessage(int clientId,
                                string name)
    {
        this.clientId = clientId;
        this.name =name;
    }
}
pantonis
  • 5,601
  • 12
  • 58
  • 115

1 Answers1

7

Yes, this is correct: protobuf-net cannot successfully round-trip a get-only property such as your ClientId, and so will throw an exception trying to construct a contract that explicitly requires such a property to be serialized.

It is not alone in this limitation. I notice that you are marking your type with data contract attributes. If I try to serialize an instance of it with DataContractSerializer, it fails with an equivalent exception:

System.Runtime.Serialization.InvalidDataContractException was caught
  Message="No set method for property 'ClientId' in type 'Question40276317.V1.TestMessage'."
  Source="System.Runtime.Serialization"

If you simply want to skip get-only properties then mark them with [IgnoreDataMember]. If you want to serialize and deserialize them successfully, you have the following options.

Firstly, protobuf-net needs to be able to construct your object. Unlike Json.NET it will not call a parameterized constructor so the simplest thing to do is to add a parameterless constructor. It could be private or protected (on the full framework) as long as it exists. Alternatively you could set [ProtoContract(SkipConstructor = true)], however that doesn't work on all frameworks or in partial trust situations.

Next, you need to make the properties settable somehow. One solution would be to add private setters:

[DataContract]
public class TestMessage
{
    private int clientId;

    [DataMember(Order = 1)]
    public int ClientId
    {
        get { return clientId; }
        private set { clientId = value; }
    }

    private string name;

    [DataMember(Order = 2)]
    public string Name
    {
        get { return name; }
        private set { name = value; }
    }

    protected TestMessage() { }

    public TestMessage(int clientId, string name)
    {
        this.clientId = clientId;
        this.name = name;
    }
}

Another would be to mark the underlying fields rather than the properties with data contract attributes:

[DataContract]
public class TestMessage
{
    [DataMember(Name = "ClientId", Order = 1)]
    private int clientId;

    public int ClientId
    {
        get { return clientId; }
    }

    [DataMember(Name = "Name", Order = 2)]
    private string name;

    public string Name
    {
        get { return name; }
    }

    protected TestMessage() { }

    public TestMessage(int clientId, string name)
    {
        this.clientId = clientId;
        this.name = name;
    }
}

Alternatively, if you really want the values for clientId and name to be immutable after construction, you're going to need a serialization surrogate, like so:

public class TestMessage
{
    private readonly int clientId;

    public int ClientId
    {
        get { return clientId; }
    }

    private readonly string name;

    public string Name
    {
        get { return name; }
    }

    public TestMessage(int clientId, string name)
    {
        this.clientId = clientId;
        this.name = name;
    }
}

[DataContract]
internal class TestMessageSurrogate
{
    public static implicit operator TestMessageSurrogate(TestMessage message)
    {
        if (message == null)
            return null;
        return new TestMessageSurrogate { ClientId = message.ClientId, Name = message.Name };
    }

    public static implicit operator TestMessage(TestMessageSurrogate message)
    {
        if (message == null)
            return null;
        return new TestMessage(message.ClientId, message.Name);
    }

    [DataMember(Order = 1)]
    public int ClientId { get; set; }

    [DataMember(Order = 2)]
    public string Name { get; set; }
}

Then, before serialization, do:

ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(TestMessage), true).SetSurrogate(typeof(TestMessageSurrogate));

By using a surrogate, you also avoid the need for any parameterless constructor.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • What if I'm not interested in deserializing them, but only in serializing? There is a specific reason why I would need only serialization, that is produce a fingerprint hashing for diffing. Really there is no way of telling protobuf-net to just serialize it? – alelom Jul 19 '19 at 15:16
  • Also, in my case I'm working with legacy, unmodifiable code, and the surrogate too won't work because the type defining read-only property is an interface, propagating to a myriad of other types. – alelom Jul 19 '19 at 15:24
  • See my question here: https://stackoverflow.com/questions/57116127/object-fingerprinting-serialization-untouchable-legacy-code-getter-only-aut – alelom Jul 19 '19 at 17:02