如何编译C#版本的Protocol Buffers与gRPC服务端,客户端代码 - Go语言中文社区

如何编译C#版本的Protocol Buffers与gRPC服务端,客户端代码


定义Protocol Buffers

message.proto

syntax = "proto3";
package Greet;

// The request message containing the user's name.
message HelloRequest {
    string name = 1;
}

// The response message containing the greetings.
message HelloReply {
    string message = 1;
}

greet.proto

syntax = "proto3";
package Greet;

option csharp_namespace = "Greet";

import "message.proto";

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

csharp_namespace这个是针对C#独有的可选配置,如果namespace与package相同,可以不用写。

使用protoc手动编译

protoc.exe可以在以下地址下载:https://github.com/protocolbuffers/protobuf/releases

如果使用nuget安装过Google.Protobuf.Tools,在这个目录也可以找到protoc.exe
%UserProfile%.nugetpackagesGoogle.Protobuf.Tools3.7.0toolswindows_x64protoc.exe

最简单的编译bat可以这样写:

protoc --csharp_out=./ greet.proto

--csharp_out是必选参数, 还有一个-I为可选参数,默认为当前目录,指定代码目录

如果需要同时编译服务端和客户端代码,需要grpc_csharp_plugin,可以在Grpc.Tools nuget包中找到:
%UserProfile%.nugetpackagesGrpc.Tools1.20.0toolswindows_x64grpc_csharp_plugin.exe

set PLUGIN=%UserProfile%.nugetpackagesGrpc.Tools1.20.0toolswindows_x64grpc_csharp_plugin.exe
D:Grpcprotoc-3.7.1-win64binprotoc.exe --csharp_out=./ greet.proto --grpc_out ./ --plugin=protoc-gen-grpc=%PLUGIN%

使用Visual Studio和Grpc.Tools自动编译

Grpc.Tools在1.17版之后,可以与MSBuild集成,自动根据proto文件编译生成C#代码。

生成的代码可以在obj/Debug/TARGET_FRAMEWORK文件夹中找到。

配置方式:

  • 添加Grpc.Tools nuget包
  • 在proto文件的属性中,将Build Action修改为Protobuf compiler
  • Build Action如果设置为Content,Custom Tool设置为MSBuild:Compile也可

注意:如果proto文件中使用了import导入其他proto文件,需要写入在Visual Studio中的完整路径,例如:

import "Protos/message.proto";
版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/13c011bd854c
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-02 14:53:09
  • 阅读 ( 1673 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢