C#中使用gRPC
c#
grpc
由於有NuGet,使得C#在配置專案時非常簡單。
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的內容,更多相關內容請關注PHP中文網(www.php.cn)!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)