[.Net Core] - create gRPC service and client in the .NET Core

gRPC official website: https://grpc.io/

1. Create a server

GRPC Server 1.1 to create a project based on ASP.NET Core Web Application template.

1.2 Compile and run

2. Create a client

2.1-based project to create gRPC Client Console Application template, and install the package Nuget ( Google.Protobuf, GRPC, Grpc.Core, Grpc.Tools ).

2.2 copy of the Server project Protos / greet.proto file to the Client project

2.3 Update Client Program.cs file, create a connection to invoke the service side.

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/

Guess you like

Origin www.cnblogs.com/jinzesudawei/p/11080542.html