1. 在NuGet中增加ProtocolBuffer和gRPC援用
protocol buffer 3.0版本,在NuGet插件界面挑选Include Prerelease,查找google protocol buffer。
假如不挑选include rerelease,查找到的protocol buffer是2.4的,没法编译
2. 定义proto
设想proto协定文件,包含效劳协定和数据。gRPC必需运用protocol buffer3.0版本,所以syntax设置为proto3。
Greeter是效劳称号
HelloRequest是要求数据
HelloReply是复兴数据
syntax = "proto3";option java_multiple_files = true;option java_package = "io.grpc.examples.helloworld"; option java_outer_classname = "HelloWorldProto";option objc_class_prefix = "HLW"; package helloworld; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name.message HelloRequest { string name = 1; } // The response message containing the greetingsmessage HelloReply { string message = 1; }
3. 生成proto接见类
定义proto文件后,经由过程protocol buffer3.0供应的protoc.exe东西生成接见类。这里运用gRPC定义的protoc的C#插件grpc_csharp_plugin.exe,而不是运用protoGen.exe。
将以下几个文件放在同一个文件夹中:
grpc_csharp_plugin.exehelloworld.protoprotoc.exe
建立一个bat文件,编写以下命令行:
protoc.exe -I=. --csharp_out=. --grpc_out=. --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe helloworld.proto
实行bat文件,获得proto的接见类:
helloworld.cshelloworldGrpc.cs
4. 建立C#项目
将两个接见类文件增加到C#项目中,将gRPC的C# example拷贝到Program.cs中,编译经由过程。
以上就是C#中运用gRPC的内容,更多相关内容请关注ki4网(www.ki4.cn)!