[.Net Core] - 在 .NET Core 中建立 gRPC 服务端和客户端

gRPC 官网:https://grpc.io/ui

1. 建立服务端spa

1.1 基于 ASP.NET Core Web 应用程序模板建立 gRPC Server 项目。code

1.2 编译并运行blog

2. 建立客户端rpc

2.1 基于控制台应用程序模板建立 gRPC Client 项目,并安装 Nuget 包(Google.Protobuf,Grpc,Grpc.Core,Grpc.Tools)。get

2.2 拷贝 Server 项目中的 Protos/greet.proto 文件到 Client 项目中string

2.3 更新客户端 Program.cs 文件,建立与服务端的链接与调用。it

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 编译并运行io

参考资料编译

https://grpc.io/docs/quickstart/csharp/

相关文章
相关标签/搜索