gRPC官方快速上手学习笔记(c#版)
2017-10-12 00:00
423 查看
上手前准备工作
支持操作系统: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[/code] 打开下载的demo文C:\Users\YPF\Desktop\grpc进入目录examples/csharp/helloworldBuild 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 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 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.csclass 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.cspublic 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/DebugGreeterServer.exe运行客户端cd GreeterClient/bin/DebugGreeterClient.exe参考文章:grpc官方文档-gRPC官方文档中文版原文地址:http://www.cnblogs.com/ypfnet/p/7606638.html.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注
相关文章推荐
- gRPC官方快速上手学习笔记(c#版)
- 【学习笔记】AS3快速上手笔记
- MySql官方手册学习笔记1 MySql简单上手
- C#快速学习笔记(译)续一
- SQLServer\framework启动报异常:Module的类型初始值设定项引发异常 [转]QQ空间、新浪微博、腾讯微博等一键分享API链接代码 window下 mongodb快速安装 数据表查询所有子/父节点 C#学习笔记1
- Struts学习笔记之快速上手
- NodeJS学习系列课程笔记(NodeJs 快速上手+用法示例)
- cocos学习笔记:快速上手
- [Docker]Docker快速上手学习笔记
- 【Keras】中文文档学习笔记-快速上手Keras
- Python编程快速上手——让繁琐工作自动化学习笔记
- C#快速学习笔记(译)
- [置顶] Qt Quick学习笔记之Qt开发环境快速上手
- c#中开发ActiveX的学习笔记
- C#学习笔记 is 和 as
- React官方文档笔记之快速入门
- C#学习笔记01:基本数据类型
- C#学习笔记(三)
- C#学习笔记10-重写、覆盖和重载
- C#学习笔记(二)——变量和表达式