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)!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

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

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

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

Dreamweaver CS6
視覺化網頁開發工具

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

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。
