By jjxtra

2019-11-08 14:23:25 8 Comments

I want to use gRPC with .NET core 3 in an 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.


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.


@poke 2019-11-08 15:02:21

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.

@jjxtra 2019-11-08 15:04:49

Thanks I will have a watch, hopefully most of it can be automated

@jjxtra 2019-11-08 15:09:19

Video 1 hour and 2 minute mark is where he starts talking about code first

@jjxtra 2019-11-08 15:17:16

The client side piece looks perfect, just like .NET remoting, still trying to find where he sets up the server :)

@poke 2019-11-08 15:18:55

I think it’s best to check out the documentation for that. There is a step-by-step explanation on that :)

@jjxtra 2019-11-08 15:20:22

services.AddCodeFirstGrpc is the magic sauce! No .proto files needed from what I can tell :)

@Marc Gravell 2019-11-08 16:02:28

@jjxtra there's a "getting started" and all of the examples from the talks, on GitHub

@jjxtra 2019-11-08 18:52:55

@MarcGravell all very helpful and well documented, thanks for pointing me to the right stuff :)

Related Questions

Sponsored Content

44 Answered Questions

[SOLVED] How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

  • 2008-09-29 22:30:28
  • mistrmark
  • 1113489 View
  • 1912 Score
  • 44 Answer
  • Tags:   c# .net excel file-io

1 Answered Questions

Create REST controller & classes from proto files

1 Answered Questions

C# gRPC service without proto file

  • 2020-05-08 22:27:23
  • andkorsh
  • 27 View
  • 0 Score
  • 1 Answer
  • Tags:   c# .net grpc

1 Answered Questions

[SOLVED] How to add an existing .proto file as link to a .net core project

2 Answered Questions

1 Answered Questions

[SOLVED] How to share gRPC proto data between client and server

1 Answered Questions

2 Answered Questions

Sponsored Content