Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use gRPC in C #

Shulou Source: shulou.com Published: 2022-06-02 07:36:51 09月24日 Update

This article shows you how to use gRPC in C#. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Add ProtocolBuffer and gRPC references to NuGet

Protocol buffer version 3.0, select Include Prerelease in the NuGet plug-in interface and find google protocol buffer.

If you do not select include rerelease, the found protocol buffer is 2.4, and example that passes through gRPC cannot be compiled.

two。 Define proto

Design proto protocol files, including service agreements and data. GRPC must use the protocol buffer3.0 version, so syntax is set to proto3.

Greeter is the name of the service

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"; 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. Generate proto access classes

After defining the proto file, 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.

Put the following files in the same folder:

Grpc_csharp_plugin.exehelloworld.protoprotoc.exe

Create a bat file and write the following command line:

Protoc.exe-Illumination. -- csharp_out=. -- grpc_out=. -- plugin=protoc-gen-grpc=grpc_csharp_plugin.exe helloworld.proto

Execute the bat file to get the access class for proto:

The above content of helloworld.cshelloworldGrpc.cs is how to use gRPC in C#. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Files clips data content skills plug-ins versions knowledge services generation selection concise concise name commands that is tools folders articles more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Shulou Information MariaDB Docker