1

Risking to violate etiquette (my apologies if that is improper), I like to bump this question because it has been floating around for a while without a single answer. I believe this is actually a more serious bug as part of the Protobuf-net library (am willing to apologize a thousand times if it turns out not to be):

I still get above error when trying to deserialize a value type which is part of a DynamicType object. Here are the methods used for serialization/deserialization and the specific class which is being serialized and deserialized along with some simple test code:

[ProtoContract]
public class ZmqMessage
{
    [ProtoMember(1)]
    public ZmqMessageType MessageType { get; set; }
    [ProtoMember(2, DynamicType = true)]
    public object MessageBody { get; set; }

    public ZmqMessage()
    { }

    public ZmqMessage(ZmqMessageType zmqMessageType, object messageBody)
    {
        this.MessageType = zmqMessageType;
        this.MessageBody = messageBody;
    }
}



public static class ProtoBuf
{
    public static byte[] Serialize<T>(T serializeThis)
    {
        using (var stream = new MemoryStream())
        {
            Serializer.Serialize<T>(stream, serializeThis);
            return stream.GetBuffer();
        }
    }

    public static T Deserialize<T>(byte[] byteArray)
    {
        using (var stream = new MemoryStream(byteArray))
        {
            return Serializer.Deserialize<T>(stream);
        }
    }
}

And here some test cases:

ZmqMessage testMessage = new ZmqMessage(ZmqMessageType.ControlMessage, "Test");
byte[] byteMessage = ProtoBuf.Serialize<ZmqMessage>(testMessage);
ZmqMessage deserializedMessage = ProtoBuf.Deserialize<ZmqMessage>(byteMessage);

ZmqMessage testMessage = new ZmqMessage(ZmqMessageType.ControlMessage, (int) 1);
byte[] byteMessage = ProtoBuf.Serialize<ZmqMessage>(testMessage);
ZmqMessage deserializedMessage = ProtoBuf.Deserialize<ZmqMessage>(byteMessage);

The first testMessage deserializes just fine, while the second (using an int in messageBody) throws above error message.

enter image description here

Matt
  • 7,004
  • 11
  • 71
  • 117
  • 1
    I will have to investigate, although one cause of this was fixed a few days ago (inheritance, not dynamic) – Marc Gravell Aug 01 '12 at 20:38
  • @MarcGravell, would you know of a workaround in the meantime? Thanks – Matt Aug 02 '12 at 06:07
  • 1
    not without looking at it first, no – Marc Gravell Aug 02 '12 at 06:26
  • @MarcGravell, I attached a screenshot of the error details in case that helps. Thanks a lot for wanting to look into it. – Matt Aug 02 '12 at 06:45
  • @MarcGravell, I may have actually found the problem, I did not mark the members of the class, which I stuck into the DynamicType, as [ProtoMember(n)] nor the class as [ProtoContract]. I was not aware this is required for sub classes especially when using the DynamicType. I apologize a thousand time, apparently there is no bug . – Matt Aug 02 '12 at 07:11
  • is there a way I can delete this whole question or do you recommend to let it stand? – Matt Aug 02 '12 at 07:12
  • 1
    let it stand; at the very minimum it sounds like some better error messages are required. A warning though: you mention "subclasses"; `DynamicType` ***does not*** play nicely with inheritance at the moment; I have some outstanding work to do there. – Marc Gravell Aug 02 '12 at 07:22
  • thanks, will also run more tests, but marking the sub class appropriately it worked well so far. But I will be careful. – Matt Aug 02 '12 at 07:29

1 Answers1

0

Solved the problem, I forgot that I had to mark sub-classes as [ProtoContract] and its members as [ProtoMembers(n)] as well, even when passing them into a "DynamicType". Please note Marc Gravell's comments above in regards to Dynamic Type and inheritance.

This was not a bug, my apologies for suggesting it as a possibility.

Matt
  • 7,004
  • 11
  • 71
  • 117