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

左心房为你撑大大i 提交于 2019-11-29 10:59:10

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!