Search code examples
c#protocol-buffersgrpc

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


Solution

  • 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