22

I want to use gRPC with .NET in an asp.net core web application. How do I generate the necessary .proto file from an existing C# class and model objects? I don't want to re-write a .proto file that mirrors the existing code, I want the .proto file to be auto-generated from the class and model objects.

I call this method to register my service class.

builder.MapGrpcService<MyGrpcService>();

public class MyGrpcService
{
    public Task<string> ServiceMethod(ModelObject model, ServerCallContext context)
    {
        return Task.FromResult("It Worked");
    }
}

ModelObject has [DataContract] and [DataMember] with order attributes.

Is this possible? Every example I see online starts with a .proto file. I've already defined my desired service methods in the MyGrpcService class. But maybe this is just backwards to what is the standard way of doing things...

Something like the old .NET remoting would be ideal where you can just ask for an interface from a remote end point and it magically uses gRPC to communicate back and forth, but maybe that is too simplistic a view.

jjxtra
  • 20,415
  • 16
  • 100
  • 140
  • I found [this video](https://www.youtube.com/watch?v=QyxCX2GYHxk&t=1501s) very helpful in getting started with gRPC. – RobIII Nov 08 '19 at 14:57
  • 1
    Yep, the "standard way of doing things" with gRPC is to specify the .proto spec first and then use a code generator to create the necessary code. – phuzi Nov 08 '19 at 15:00
  • Thanks for the video link, I've already spent the last few days getting familiar with how .proto files and gRPC works, just have a large number of interfaces and model objects I was hoping to get some auto-tooling working with... but maybe there is no getting around rewriting a wrapper and generating .proto files with a tool or failing that manually hand writing the .proto files. – jjxtra Nov 08 '19 at 15:01
  • For C# to C# interop, AddCodeFirstGrpc from protobuf-net.Grpc is the way to go. The client can then access Grpc service with just an interface, ip and port using protobuf-net.Grpc. If you you need interop of some other language calling your C# grpc service, you can generate a .proto from C# code, see https://stackoverflow.com/questions/1334659/how-do-i-generate-a-proto-file-from-a-c-sharp-class-decorated-with-attributes. – jjxtra Nov 25 '19 at 23:45
  • Can I ask one more thing? How does client code look like? thanks in advance – Stefano Vuerich Aug 14 '20 at 08:28

1 Answers1

19

You can use Marc Gravell’s protobuf-net.Grpc for this. Having a code-first experience when building gRPC services is the exact use case why he started working on it. It builds on top of protobuf-net which already adds serialization capabilities between C# types and protobuf.

Check out the documentation to see how to get started using the library, or even watch Marc present this topic in one of the following recordings of his talk “Talking Between Services with gRPC and Other Tricks”:

I think he actually updated the one in September for the release bits of .NET Core 3.0, so that would probably be the more updated version.

There are also a few code samples to see how this looks like when you set it up.

poke
  • 369,085
  • 72
  • 557
  • 602
  • Thanks I will have a watch, hopefully most of it can be automated – jjxtra Nov 08 '19 at 15:04
  • 1
    Video 1 hour and 2 minute mark is where he starts talking about code first – jjxtra Nov 08 '19 at 15:09
  • The client side piece looks perfect, just like .NET remoting, still trying to find where he sets up the server :) – jjxtra Nov 08 '19 at 15:17
  • I think it’s best to check out the documentation for that. There is a step-by-step explanation on that :) – poke Nov 08 '19 at 15:18
  • services.AddCodeFirstGrpc is the magic sauce! No .proto files needed from what I can tell :) – jjxtra Nov 08 '19 at 15:20
  • 1
    @jjxtra there's a "getting started" and all of the examples from the talks, on GitHub – Marc Gravell Nov 08 '19 at 16:02
  • @MarcGravell all very helpful and well documented, thanks for pointing me to the right stuff :) – jjxtra Nov 08 '19 at 18:52
  • 4
    But the question is already exists that how can we create protobuf from a code first grpc in .net. To be used by other platforms. – Farshid Saberi Jan 23 '21 at 21:14
  • @FarshidSaberi check out [IndependentReserve.Grpc.Tools](https://www.nuget.org/packages/IndependentReserve.Grpc.Tools#readme-body-tab). It generates (among other things) `*.proto` files from plain .NET interfaces. – Ed'ka Mar 14 '23 at 21:47