分成两个地方
1.服务端
2.客户端

服务端

步骤1:添加gRPC项目

image.png
项目中两个主要的文件需要关注
greet.proto
定义接口的相关信息

GreeterService.cs
实现接口的操作

image.png

步骤2: 添加protpo示例

1.添加“协议缓存文件”
image.png
2.在项目文件中,手动添加相应proto文件的ItemGroup
image.png

步骤3:Service文件的实现

image.png

客户端

步骤1:在项目中相关package

image.png

步骤2:添加相关client

在项目文件中,添加你要请求的服务的ItemGroup,GrpcServices需改为Client
image.png

  1. <ItemGroup>
  2. <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup>

步骤3:调用实例

image.png

  1. static async Task Main(string[] args)
  2. {
  3. // The port number(5001) must match the port of the gRPC server.
  4. using var channel = GrpcChannel.ForAddress("https://localhost:5001");
  5. var client = new Greeter.GreeterClient(channel);
  6. var reply = await client.SayHelloAsync(new HelloRequest { Name = "阴影" });
  7. Console.WriteLine("Greeting: " + reply.Message);
  8. Console.WriteLine("Press any key to exit...");
  9. Console.ReadKey();
  10. }

参考

ASP.Net Core 3.1 使用gRPC入门指南 .NET 上的 gRPC 的简介 Asp.Net Core Grpc 入门实践