Search code examples
c#deserializationprotocol-buffersprotobuf-netconfluent-schema-registry

Deserialization of protobuf


I have a .proto file that I have traditionally been using with protogen.exe to generate a .cs file for the relevant class. The class generated implements Protobuf.IExtensible. However, I now need to deserialise protobuf data coming from Kafka, from a topic that has the same .proto file in its schema registry. From what I can see, the Confluent.SchemaRegistry.Serdes.ProtobufDeserializer expects the underlying type to implement Google.Protobuf.IMessage<T> instead. I haven't been able to find good information linking IExtensible and IMessage. Is there a way for me to deserialise the object, using the same class I have always been using? I have tried Protobuf.Serializer.Deserialize and it gives me ProtoException: "Invalid field in source data: 0".


Solution

  • There are (at least) two separate implementations of Protobuf in regular use in .NET code:

    • Google's own implementation, which has the IMessage<T> API and is intended for schema-first scenarios, and is idiomatic protobuf that just happens to be in .NET / C#
    • protobuf-net, a 3rd party implementation, which supports schema-first but which largely targets code-first scenarios, and is idiomatic .NET serialization that just happens to be dealing with protobuf

    If the Confluent API wants IMessage<T>, then you should use the Google.Protobuf library and associated tools to process the schema, and work from there. This processes the exact same binary payloads, but the API has some differences.