By jjxtra


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

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

1 comments

@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
  • 1050479 View
  • 1809 Score
  • 44 Answer
  • Tags:   c# .net excel file-io

16 Answered Questions

[SOLVED] How to get the type of T from a member of a generic class or method?

  • 2009-02-17 15:24:36
  • Patrick Desjardins
  • 597468 View
  • 648 Score
  • 16 Answer
  • Tags:   c# .net generics

1 Answered Questions

How to generate gRPC services and reuse existed DTOs in java with gradle?

1 Answered Questions

1 Answered Questions

[SOLVED] Protobuf-net .proto file generation for inheritance

2 Answered Questions

Sponsored Content