Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

c# - 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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...