Search code examples
c#.netprotocol-buffersprotobuf-net

Self describing messages on network with ProtoBuf-Net


I want to use protobuf-net for the protocol for a network socket communication between a client and a server.

There are about a dozen or so different class types a message can be of (they all extend the same base class). Is it possible to decode the message before the exact type is known? How can this be achieved?

Thanks in advance


Solution

  • It sounds to me like you want:

    [ProtoContract]
    [ProtoInclude(1, typeof(Foo)]
    ...
    [ProtoInclude(N, typeof(Bar)]
    class SomeBase {}
    
    [ProtoContract]
    class Foo : SomeBase {...}
    
    ...
    
    [ProtoContract]
    class Bar : SomeBase {...}
    

    Then use Deserialize<SomeBase>(...). This will be mapped as a oneof discriminated union in ".proto" terms.