pantonis pantonis - 1 month ago 22
C# Question

Protobuf-net fails to serialize property with only getter - Cannot apply changes to property

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;
}
}

dbc dbc
Answer

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.