Search code examples
protobuf-netprotobuf-net.grpc

Mixing Code-First gRPC with Contract-First gRPC


Code-First = protobuf-net.Grpc.AspNetCore

Contract-First = Grpc.AspNetCore

I followed the example projects and they communicate without error to their respective server client. However, if I mix and match (code-first to contract-first or vice versa) I get a method not implemented exception. The proto file matches the code first structure.

Is this a limitation of these approaches?

If so, how do we use Contract-First without writing mapper classes from existing entities to the ones defined in the protos file?

(Github solution of Greeter sample for both)


Solution

  • Defining the Code-First service with the correct annotations works:

    namespace CodeFirstLib
    {
        [ProtoContract()]
        public partial class HelloRequest
        {
            [ProtoMember(1, Name = @"name")]
            public string Name { get; set; } = "";
        }
    
        [ProtoContract()]
        public partial class HelloReply
        {
            [ProtoMember(1, Name = @"message")]
            public string Message { get; set; } = "";
    
        }
    
        [ServiceContract(Name = @"greet.Greeter")]
        public partial interface IGreeter
        {
            ValueTask<HelloReply> SayHelloAsync(HelloRequest value, CallContext context = default);
        }
    }