How do I generate .proto files or use 'Code First gRPC' in C#
Asked Answered
S

2

24

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.

Shipmate answered 8/11, 2019 at 14:23 Comment(5)
I found this video very helpful in getting started with gRPC.Gotha
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.Danged
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.Shipmate
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 #1335159.Shipmate
Can I ask one more thing? How does client code look like? thanks in advanceEngrave
M
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.

Memnon answered 8/11, 2019 at 15:2 Comment(9)
Thanks I will have a watch, hopefully most of it can be automatedShipmate
Video 1 hour and 2 minute mark is where he starts talking about code firstShipmate
The client side piece looks perfect, just like .NET remoting, still trying to find where he sets up the server :)Shipmate
I think it’s best to check out the documentation for that. There is a step-by-step explanation on that :)Memnon
services.AddCodeFirstGrpc is the magic sauce! No .proto files needed from what I can tell :)Shipmate
@Shipmate there's a "getting started" and all of the examples from the talks, on GitHubDud
@MarcGravell all very helpful and well documented, thanks for pointing me to the right stuff :)Shipmate
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.Erida
@FarshidSaberi check out IndependentReserve.Grpc.Tools. It generates (among other things) *.proto files from plain .NET interfaces.Aphasia
N
1

You can use Github-Copilot and ask following questions:

Please convert to protobuf: "paste here your class definition"

If the class is not that long, Github will do a near-perfect proposal. Copy and paste this to your proto file. This works for C# and C++ and probably other languages also.

Newscast answered 20/2 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.