日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

gRPC官方快速上手学习笔记(c#版)

發布時間:2023/12/4 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 gRPC官方快速上手学习笔记(c#版) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上手前準備工作

支持操作系統: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
  • 打開下載的demo文C:\Users\YPF\Desktop\grpc

  • 進入目錄examples/csharp/helloworld

  • Build the example

  • 使用Visual Studio打開Greeter.sln

  • 在該項目的解決右鍵重新生成解決方案

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

    運行 a gRPC application

  • 運行服務

    > cd GreeterServer/bin/Debug> GreeterServer.exe

  • 運行客戶端

    > cd GreeterClient/bin/Debug> GreeterClient.exe

  • 更新 a gRPC service

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

    // The greeting service definition.service Greeter { ?// Sends a greetingrpc SayHello (HelloRequest) returns (HelloReply) {} ?// Sends another greetingrpc SayHelloAgain (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; }

    生成 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

    參考文章:
    • grpc官方文檔
      -gRPC官方文檔中文版

    原文地址:http://www.cnblogs.com/ypfnet/p/7606638.html


    .NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注

    總結

    以上是生活随笔為你收集整理的gRPC官方快速上手学习笔记(c#版)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。