gRPC官方快速上手學習筆記(c#版)

来源:http://www.cnblogs.com/ypfnet/archive/2017/09/28/7606638.html
-Advertisement-
Play Games

上手前準備工作 支持操作系統:windows、OS X、Linux。實例採用.net、.net core sdk。 The .NET Core SDK command line tools. The .NET framework 4.5 (for OS X and Linux, the open s ...


上手前準備工作

支持操作系統:windows、OS X、Linux。實例採用.net、.net core sdk。

  • The .NET Core SDK command line tools.
  • The .NET framework 4.5 (for OS X and Linux, the open source .NET Framework implementation, “Mono”, at version 4+, is suitable)
  • Git (to download the sample code)

在windows系統開發環境, 採用 Visual Studio開發工具, 需要滿足以下要求:

  • .NET Framework 4.5+
  • Visual Studio 2013 or 2015.
  • Git (to download the sample code)

    在OS X 系統開發環境, 採用Xamarin Studio開發工具, 需要滿足以下要求:

  • Mono 4.4.2+ (or Mono 4+ is sufficient if you manually update NuGet to version 2.12+)
  • Xamarin Studio 6.0+
  • Git (to download the sample code)

    在 Linux 系統開發環境, 採用 the Monodevelop IDE,需要滿足以下要求 :

  • Mono 4.4.2+ (or Mono 4+ is sufficient if you manually update nuget to version 2.12+)
  • MonoDevelop 5.9+
  • A NuGet executable, at version 2.12+ (you’ll need to restore NuGet package dependencies from the command line)
  • Git (to download the sample code)

下載官方demo

git clone -b v1.6.x https://github.com/grpc/grpc
  1. 打開下載的demo文C:\Users\YPF\Desktop\grpc
  2. 進入目錄examples/csharp/helloworld

Build the example

  1. 使用Visual Studio打開Greeter.sln
  2. 在該項目的解決右鍵重新生成解決方案

項目會自動使用NuGet進行必要的package的安裝。

運行 a gRPC application

  1. 運行服務

    > cd GreeterServer/bin/Debug
    > GreeterServer.exe

  2. 運行客戶端

    > cd GreeterClient/bin/Debug
    > GreeterClient.exe

更新 a gRPC service

打開目錄examples/protos/helloworld.proto
將原來的文件修改為如下並保存:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

生成 gRPC code

在demo的根目錄(examples/csharp/helloworld)下執行如下命令:

packages\Grpc.Tools.1.6.1\tools\windows_x86\protoc.exe -I../../protos --csharp_out Greeter --grpc_out Greeter ../../protos/helloworld.proto --plugin=protoc-gen-grpc=packages/Grpc.Tools.1.6.1/tools/windows_x86/grpc_csharp_plugin.exe

這裡的Grpc.Tools.1.6.1這個命令必須是跟項目中使用NuGet安裝的版本一致,否則會報錯。

更新並從新運行

修改服務端代碼

GreeterServer/Program.cs

class GreeterImpl : Greeter.GreeterBase
{
    // Server side handler of the SayHello RPC
    public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
    }

    // Server side handler for the SayHelloAgain RPC
    public override Task<HelloReply> SayHelloAgain(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply { Message = "Hello again " + request.Name });
    }
}

修改服務端代碼

GreeterClient/Program.cs

public static void Main(string[] args)
{
    Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);

    var client = new Greeter.GreeterClient(channel);
    String user = "you";

    var reply = client.SayHello(new HelloRequest { Name = user });
    Console.WriteLine("Greeting: " + reply.Message);
    
    var secondReply = client.SayHelloAgain(new HelloRequest { Name = user });
    Console.WriteLine("Greeting: " + secondReply.Message);

    channel.ShutdownAsync().Wait();
    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}

運行 a gRPC application

  • 運行服務

    cd GreeterServer/bin/Debug
    GreeterServer.exe

  • 運行客戶端

    cd GreeterClient/bin/Debug
    GreeterClient.exe

參考文章:

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1.創建Bundle php app/console generate:bundle --namespace=Home/IndexBundle --format=yml 創建bundle會更新app文件夾下的 appKernel.php config/routing.yml ...
  • code: 文件內容:(yesterday) 感想: 思路很簡單就是打開源文件,然後迴圈,把源文件要替換的內容替換再寫入新文件! ...
  • 咱們繼續看uvw的源碼,這次看的東西比較多,去除底層的一些東西,很多代碼都是連貫的,耦合度也比較高了。主要包括下麵幾個文件的代碼: underlying_type.hpp resource.hpp loop.hpp handle.hpp stream.hpp tcp.hpp 代碼我就不都貼出了,說到 ...
  • using System.Collections; ArrayList arr = new ArrayList(); arr.Add("123"); arr.Add("abc"); string s = "abc"; bool b = arr.Contains(s); Console.WriteLi ...
  • [導航頁 LeetCode專題 Python實現][1] [1]: http://www.cnblogs.com/exploitht/p/7488742.html 相關代碼已經上傳到github: "https://github.com/exploitht/leetcode python" 文中代碼 ...
  • 隨著目前Python行業的薪資水平越來越高,很多人想加入該行業拿高薪。有沒有想通過視頻教程入門的同學們?這份Python3教程全集等你來學習啦! ...
  • 就是用eclipse編程,要先安裝jre(java runtime environment)。 java是一種強類型的語言,在使用變數之前必須申明變數類型。 ...
  • 前一講主要說了jenkins分發的好處《jenkins~集群分發功能和職責處理》,它可以讓具體的節點乾自己具體的事,比如windows環境下的節點,它只負責編譯,發佈windows的生態環境的項目;而linux節點主要負責和它相關的項目,如nodejs,.net core,java,php,pyth ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...