作者:最美的你 | 来源:互联网 | 2023-05-17 22:31
gRPC 官网:https://grpc.io/
1. 创建服务端
1.1 基于 ASP.NET Core Web 应用程序模板创建 gRPC Server 项目。
1.2 编译并运行
2. 创建客户端
2.1 基于控制台应用程序模板创建 gRPC Client 项目,并安装 Nuget 包(Google.Protobuf,Grpc,Grpc.Core,Grpc.Tools)。
2.2 拷贝 Server 项目中的 Protos/greet.proto 文件到 Client 项目中
2.3 更新客户端 Program.cs 文件,创建与服务端的连接与调用。
static void Main(string[] args)
{
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
var client = new Greeter.GreeterClient(channel);
string user = "jinzesudawei";
var reply = client.SayHello(new HelloRequest { Name = user });
Console.WriteLine("First Time - Hello: " + reply.Message);
var secOndReply= client.SayHello(new HelloRequest { Name = user });
Console.WriteLine("第二次 - 你好: " + secondReply.Message);
channel.ShutdownAsync().Wait();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
2.4 编译并运行
参考资料
https://grpc.io/docs/quickstart/csharp/
https://docs.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.0&tabs=visual-studio