Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the gRPC tools to generate code

I've read the tutorial and I'm able to generate the .cs file but it doesn't include any of my service or rpc definitions.

I've added protoc to my PATH and from inside the project directory.

protoc project1.proto --csharp_out="C:\output" --plugin=protoc-gen-grpc="c:\Users\me\.nuget\packages\grpc.tools\1.8.0\tools\windows_x64\grpc_csharp_plugin.exe"

No errors output in console

like image 301
user3953989 Avatar asked Jun 04 '18 19:06

user3953989


People also ask

Is gRPC a programming language?

gRPC is a high performance, open source framework developed by Google to handle remote procedure calls (RPCs). gRPC is Google's approach to a client-server application. It lets client and server applications communicate transparently, simplifying the process for developers to build connected systems.

How is gRPC encoded?

For encoding messages, gRPC uses protocol buffers. Protocol buffers are a language-agnostic, platform-neutral, extensible mechanism for serializing structured data.

How is gRPC used?

gRPC is a robust open-source RPC (Remote Procedure Call) framework used to build scalable and fast APIs. It allows the client and server applications to communicate transparently and develop connected systems. Many leading tech firms have adopted gRPC, such as Google, Netflix, Square, IBM, Cisco, & Dropbox.


1 Answers

You need to add the --grpc_out command line option, e.g. add

--grpc_out="C:\output\"

Note that it won't write any files if you don't have any services.

Here's a complete example. From a root directory, create:

  • An empty output directory
  • A tools directory with protoc.exe and grpc_csharp_plugin.exe
  • A protos directory with test.proto as shown below:

test.proto:

syntax = "proto3";

service StackOverflowService {
  rpc GetAnswer(Question) returns (Answer);
}

message Question {
  string text = 1;
  string user = 2;
  repeated string tags = 3;
}

message Answer {
  string text = 1;
  string user = 2;
}

Then run (all on one line; I've broken it just for readability here):

tools\protoc.exe -I protos protos\test.proto --csharp_out=output
    --grpc_out=output --plugin=protoc-gen-grpc=tools\grpc_csharp_plugin.exe 

In the output directory, you'll find Test.cs and TestGrpc.cs

like image 116
Jon Skeet Avatar answered Sep 22 '22 21:09

Jon Skeet