Using GRPC in C #
My articles are the use of GRPC example, not directly compiled example, but a new project, from adding dependencies, compiling example code, execute example. This can be used as a reference for us to create our own projects. If you are familiar with GRPC build apps, you can ignore this series.
Directory
I. Overview
Second, compiling GRPC
Third, use GRPC in C #
Iv. using GRPC in C + +
Because of NuGet, C # is very simple to configure a project.
1. Add Protocolbuffer and Grpc references in NuGet
Protocol Buffer version 3.0, select Include prerelease in the NuGet plugin interface to find Google protocol buffer.
If you do not select Include rerelease, the protocol buffer found is 2.4 and cannot be compiled example through GRPC.
2. Define Proto
Design proto protocol files, including service protocols and data. GRPC must use the Protocol buffer3.0 version, so syntax set to proto3 .
Greeter is the service name
Hellorequest is the request data
Helloreply is the reply data
Syntax ="Proto3";Option Java_multiple_files =true; option java_package = "Io.grpc.examples.helloworld"; option java_outer_classname = "Helloworldproto"; option objc_class_prefix = " HLW ";p ackage 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. Generate Proto Access class
Once the proto file is defined, the access class is generated through the Protoc.exe tool provided by protocol buffer3.0. Instead of using ProtoGen.exe, the Protoc C # plug-in Grpc_csharp_plugin.exe defined by GRPC is used here.
Place the following files in the same folder:
grpc_csharp_plugin.exehelloworld.protoprotoc.exe
Create a bat file and write the following command line:
--csharp_out=. --grpc_out=. --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe helloworld.proto
Execute the bat file to get the Proto Access class:
helloworld.cshelloworldGrpc.cs
4. Create a C # project
Add two access class files to your C # project, copy grpc C # example to Program.cs, and compile through.
Using GRPC in C #