翼度科技»论坛 编程开发 .net 查看内容

.Net Core gRpc调用

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15

目录

简介


创建gRPC

创建服务端


  • vs2022直接搜索grpc默认下一步创建
创建控制台测试


  • 创建控制台
  • 引入以下dll
  1. [/code][list=1]
  2. [*]打开服务端.csproj文件,复制以下内容粘贴到客户端的.csproj文件中并修改GrpcServices=Client,客户端.csproj文件出现 None Update="Protos\greet.proto" 这组ItemGroup是可以删除的
  3. [/list][code]<ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>
复制代码
4.将服务端Protos文件夹及内容全部拷贝到客户端项目下
5.在客户端创建gRpcRequest.cs文件并增加下列代码,端口号填写服务端端口
  1. using Grpc.Net.Client;using GrpcService;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using static GrpcService.Greeter;namespace gRpcConsoleTest{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public static class gRpcRequest<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public static async Task SayHello()<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>{<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>using(var channel = GrpcChannel.ForAddress("https://localhost:7166"))<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup> {<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup><ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>GreeterClient client = new GreeterClient(channel);<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>HelloReply reply = await client.SayHelloAsync(new HelloRequest() {Name = "jjjjj" });<ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup>Console.WriteLine(reply.Message);<ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup>}<ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup><ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup>}<ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup>}}
复制代码
6.客户端Program.cs文件中加入测试代码 await gRpcRequest.SayHello();
7.服务端和客户端在想要调试的位置打上断点
8.运行服务端
9.选中客户端项目右键 -> 调试 -> 启动新实例 即可两个项目全部命中断点进行调试测试
创建自定义服务



  • 服务端

    • Protos文件夹添加 custom.proto文件并添加下列代码,重新生成项目打开项目所在文件夹,打开路径:obj\Debug\net6.0\Protos 查看CustomGrpc.cs是否存在,如果没有存在则在.csproj文件中添加:
    1. syntax = "proto3";option csharp_namespace = "Custom.Service";package custom;service CustomGreeter {<ItemGroup>
    2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    3. </ItemGroup>rpc Plus(Number) returns (NumberResult) ;}message Number {<ItemGroup>
    4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    5. </ItemGroup><ItemGroup>
    6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    7. </ItemGroup>int32 leftNumber = 1;<ItemGroup>
    8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    9. </ItemGroup><ItemGroup>
    10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    11. </ItemGroup>int32 rightNumber = 2;}<ItemGroup>
    12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    13. </ItemGroup>message NumberResult{<ItemGroup>
    14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    15. </ItemGroup><ItemGroup>
    16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    17. </ItemGroup>int32 result = 1;}
    复制代码

    • Services文件夹下添加CustomGreeterService.cs文件,namespace 与 .protos中的csharp_namespace对应
    1. using Grpc.Core;namespace Custom.Service{<ItemGroup>
    2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    3. </ItemGroup><ItemGroup>
    4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    5. </ItemGroup>public class CustomGreeterService : CustomGreeter.CustomGreeterBase<ItemGroup>
    6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    7. </ItemGroup><ItemGroup>
    8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    9. </ItemGroup>{<ItemGroup>
    10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    11. </ItemGroup><ItemGroup>
    12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    13. </ItemGroup><ItemGroup>
    14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    15. </ItemGroup><ItemGroup>
    16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    17. </ItemGroup>public override async Task Plus(Number request, ServerCallContext context) =><ItemGroup>
    18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    19. </ItemGroup><ItemGroup>
    20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    21. </ItemGroup><ItemGroup>
    22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    23. </ItemGroup><ItemGroup>
    24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    25. </ItemGroup><ItemGroup>
    26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    27. </ItemGroup><ItemGroup>
    28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    29. </ItemGroup> await Task.FromResult(new NumberResult()<ItemGroup>
    30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    31. </ItemGroup><ItemGroup>
    32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    33. </ItemGroup><ItemGroup>
    34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    35. </ItemGroup><ItemGroup>
    36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    37. </ItemGroup><ItemGroup>
    38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    39. </ItemGroup><ItemGroup>
    40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    41. </ItemGroup>{<ItemGroup>
    42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    43. </ItemGroup><ItemGroup>
    44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    45. </ItemGroup><ItemGroup>
    46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    47. </ItemGroup><ItemGroup>
    48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    49. </ItemGroup><ItemGroup>
    50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    51. </ItemGroup><ItemGroup>
    52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    53. </ItemGroup><ItemGroup>
    54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    55. </ItemGroup><ItemGroup>
    56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    57. </ItemGroup>Result = request.LeftNumber + request.RightNumber,<ItemGroup>
    58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    59. </ItemGroup><ItemGroup>
    60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    61. </ItemGroup><ItemGroup>
    62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    63. </ItemGroup><ItemGroup>
    64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    65. </ItemGroup><ItemGroup>
    66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    67. </ItemGroup><ItemGroup>
    68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    69. </ItemGroup>});<ItemGroup>
    70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    71. </ItemGroup><ItemGroup>
    72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    73. </ItemGroup>}}
    复制代码
    3.在Program.cs 中注册新创建的服务,加入下列代码:
    app.MapGrpcService();

  • 客户端

    • 将服务端的custom.proto文件拷贝到Protos文件夹内并在.csproj文件中添加(注意这里GrpcServices="Client"):
    • 重新生成项目并检查路径: obj\Debug\net6.0\Protos 是否生成对应的CustomGrpc.cs文件
    • 加入测试代码:
    1. public static async Task Plus(int leftNumber,int rightNumber){<ItemGroup>
    2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    3. </ItemGroup><ItemGroup>
    4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    5. </ItemGroup>using (var channel = GrpcChannel.ForAddress("https://localhost:7166"))<ItemGroup>
    6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    7. </ItemGroup><ItemGroup>
    8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    9. </ItemGroup>{<ItemGroup>
    10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    11. </ItemGroup><ItemGroup>
    12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    13. </ItemGroup><ItemGroup>
    14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    15. </ItemGroup><ItemGroup>
    16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    17. </ItemGroup>CustomGreeterClient client = new CustomGreeterClient(channel);<ItemGroup>
    18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    19. </ItemGroup><ItemGroup>
    20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    21. </ItemGroup><ItemGroup>
    22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    23. </ItemGroup><ItemGroup>
    24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    25. </ItemGroup>NumberResult number = await client.PlusAsync(new Custom.Service.Number() { LeftNumber = leftNumber,RightNumber = rightNumber});<ItemGroup>
    26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    27. </ItemGroup><ItemGroup>
    28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    29. </ItemGroup><ItemGroup>
    30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    31. </ItemGroup><ItemGroup>
    32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    33. </ItemGroup>Console.WriteLine(number.Result);<ItemGroup>
    34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    35. </ItemGroup><ItemGroup>
    36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    37. </ItemGroup>}}
    复制代码

    • 测试步骤与上面控制台测试一样,调试方式也一样

服务器流式处理方法

custom.proto
  1. syntax = "proto3";option csharp_namespace = "Custom.Service";package custom;service CustomGreeter {<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup>rpc SelfIncreaseServer(IntArrayModel) returns (stream BathTheCatResp); //服务端流}message BathTheCatResp{<ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup><ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup>string message = 1;}message IntArrayModel{<ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup><ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup>repeated int32 number = 1;}
复制代码
CustomGreeterService.cs
  1. using Grpc.Core;namespace Custom.Service{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class CustomGreeterService : CustomGreeter.CustomGreeterBase<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public override async Task SelfIncreaseServer(IntArrayModel request, IServerStreamWriter responseStream, ServerCallContext context)<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>{<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>foreach (var item in request.Number)<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup>{<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup><ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>Console.WriteLine($"客户端传入参数: {item}");<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>await responseStream.WriteAsync(new BathTheCatResp() { Message = item.ToString()});<ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup>await Task.Delay(1000);<ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup>}<ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup><ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup>}<ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup>}}
复制代码
gRpcRequest.cs
  1. using Custom.Service;using Grpc.Core;using Grpc.Net.Client;using GrpcService;using static Custom.Service.CustomGreeter;namespace gRpcConsoleTest{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public static class gRpcRequest<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public static async Task SelfIncreaseServe()<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>{<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>using (var channel = GrpcChannel.ForAddress("https://localhost:7166"))<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup>{<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup><ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>CustomGreeterClient client = new CustomGreeterClient(channel);<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>IntArrayModel intArray = new IntArrayModel();<ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup>for (int i = 0; i < 10; i++)<ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup><ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup>{<ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup><ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup><ItemGroup>
  122.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  123. </ItemGroup><ItemGroup>
  124.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  125. </ItemGroup><ItemGroup>
  126.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  127. </ItemGroup><ItemGroup>
  128.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  129. </ItemGroup><ItemGroup>
  130.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  131. </ItemGroup><ItemGroup>
  132.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  133. </ItemGroup>intArray.Number.Add(i);<ItemGroup>
  134.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  135. </ItemGroup><ItemGroup>
  136.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  137. </ItemGroup><ItemGroup>
  138.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  139. </ItemGroup><ItemGroup>
  140.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  141. </ItemGroup><ItemGroup>
  142.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  143. </ItemGroup><ItemGroup>
  144.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  145. </ItemGroup><ItemGroup>
  146.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  147. </ItemGroup><ItemGroup>
  148.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  149. </ItemGroup><ItemGroup>
  150.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  151. </ItemGroup> }<ItemGroup>
  152.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  153. </ItemGroup><ItemGroup>
  154.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  155. </ItemGroup><ItemGroup>
  156.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  157. </ItemGroup><ItemGroup>
  158.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  159. </ItemGroup><ItemGroup>
  160.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  161. </ItemGroup><ItemGroup>
  162.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  163. </ItemGroup><ItemGroup>
  164.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  165. </ItemGroup><ItemGroup>
  166.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  167. </ItemGroup>var batch = client.SelfIncreaseServer(intArray);<ItemGroup>
  168.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  169. </ItemGroup><ItemGroup>
  170.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  171. </ItemGroup><ItemGroup>
  172.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  173. </ItemGroup><ItemGroup>
  174.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  175. </ItemGroup><ItemGroup>
  176.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  177. </ItemGroup><ItemGroup>
  178.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  179. </ItemGroup><ItemGroup>
  180.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  181. </ItemGroup><ItemGroup>
  182.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  183. </ItemGroup>Task batchTask = Task.Run(async ()=><ItemGroup>
  184.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  185. </ItemGroup><ItemGroup>
  186.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  187. </ItemGroup><ItemGroup>
  188.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  189. </ItemGroup><ItemGroup>
  190.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  191. </ItemGroup><ItemGroup>
  192.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  193. </ItemGroup><ItemGroup>
  194.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  195. </ItemGroup><ItemGroup>
  196.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  197. </ItemGroup><ItemGroup>
  198.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  199. </ItemGroup>{<ItemGroup>
  200.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  201. </ItemGroup><ItemGroup>
  202.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  203. </ItemGroup><ItemGroup>
  204.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  205. </ItemGroup><ItemGroup>
  206.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  207. </ItemGroup><ItemGroup>
  208.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  209. </ItemGroup><ItemGroup>
  210.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  211. </ItemGroup><ItemGroup>
  212.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  213. </ItemGroup><ItemGroup>
  214.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  215. </ItemGroup><ItemGroup>
  216.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  217. </ItemGroup><ItemGroup>
  218.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  219. </ItemGroup>await foreach (var item in batch.ResponseStream.ReadAllAsync())<ItemGroup>
  220.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  221. </ItemGroup><ItemGroup>
  222.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  223. </ItemGroup><ItemGroup>
  224.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  225. </ItemGroup><ItemGroup>
  226.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  227. </ItemGroup><ItemGroup>
  228.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  229. </ItemGroup><ItemGroup>
  230.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  231. </ItemGroup><ItemGroup>
  232.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  233. </ItemGroup><ItemGroup>
  234.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  235. </ItemGroup><ItemGroup>
  236.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  237. </ItemGroup><ItemGroup>
  238.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  239. </ItemGroup>{<ItemGroup>
  240.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  241. </ItemGroup><ItemGroup>
  242.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  243. </ItemGroup><ItemGroup>
  244.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  245. </ItemGroup><ItemGroup>
  246.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  247. </ItemGroup><ItemGroup>
  248.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  249. </ItemGroup><ItemGroup>
  250.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  251. </ItemGroup><ItemGroup>
  252.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  253. </ItemGroup><ItemGroup>
  254.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  255. </ItemGroup><ItemGroup>
  256.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  257. </ItemGroup><ItemGroup>
  258.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  259. </ItemGroup><ItemGroup>
  260.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  261. </ItemGroup><ItemGroup>
  262.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  263. </ItemGroup>Console.WriteLine($"服务端相应数据: {item.Message}");<ItemGroup>
  264.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  265. </ItemGroup><ItemGroup>
  266.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  267. </ItemGroup><ItemGroup>
  268.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  269. </ItemGroup><ItemGroup>
  270.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  271. </ItemGroup><ItemGroup>
  272.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  273. </ItemGroup><ItemGroup>
  274.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  275. </ItemGroup><ItemGroup>
  276.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  277. </ItemGroup><ItemGroup>
  278.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  279. </ItemGroup><ItemGroup>
  280.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  281. </ItemGroup><ItemGroup>
  282.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  283. </ItemGroup>}<ItemGroup>
  284.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  285. </ItemGroup><ItemGroup>
  286.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  287. </ItemGroup><ItemGroup>
  288.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  289. </ItemGroup><ItemGroup>
  290.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  291. </ItemGroup><ItemGroup>
  292.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  293. </ItemGroup><ItemGroup>
  294.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  295. </ItemGroup><ItemGroup>
  296.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  297. </ItemGroup><ItemGroup>
  298.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  299. </ItemGroup>});<ItemGroup>
  300.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  301. </ItemGroup><ItemGroup>
  302.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  303. </ItemGroup><ItemGroup>
  304.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  305. </ItemGroup><ItemGroup>
  306.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  307. </ItemGroup><ItemGroup>
  308.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  309. </ItemGroup><ItemGroup>
  310.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  311. </ItemGroup><ItemGroup>
  312.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  313. </ItemGroup><ItemGroup>
  314.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  315. </ItemGroup>await batchTask;<ItemGroup>
  316.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  317. </ItemGroup><ItemGroup>
  318.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  319. </ItemGroup><ItemGroup>
  320.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  321. </ItemGroup><ItemGroup>
  322.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  323. </ItemGroup><ItemGroup>
  324.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  325. </ItemGroup><ItemGroup>
  326.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  327. </ItemGroup>}<ItemGroup>
  328.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  329. </ItemGroup><ItemGroup>
  330.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  331. </ItemGroup><ItemGroup>
  332.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  333. </ItemGroup><ItemGroup>
  334.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  335. </ItemGroup>}<ItemGroup>
  336.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  337. </ItemGroup><ItemGroup>
  338.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  339. </ItemGroup>}}
复制代码
客户端流式处理方法

custom.proto
  1. syntax = "proto3";option csharp_namespace = "Custom.Service";package custom;service CustomGreeter {<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup>rpc SelfIncreaseClient(stream BathTheCatReq) returns (IntArrayModel); //客户端流}message BathTheCatResp{<ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup><ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup>string message = 1;}message IntArrayModel{<ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup><ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup>repeated int32 number = 1;}
复制代码
CustomGreeterService.cs
  1. using Grpc.Core;namespace Custom.Service{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class CustomGreeterService : CustomGreeter.CustomGreeterBase<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public override async Task SelfIncreaseClient(IAsyncStreamReader requestStream, ServerCallContext context)<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>{<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>IntArrayModel result = new IntArrayModel();<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup>while (await requestStream.MoveNext())<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup>{<ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup><ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup>var message = requestStream.Current;<ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup><ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup>Console.WriteLine($"客户端流传入消息: {message}");<ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup><ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup><ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup>result.Number.Add(message.Id + 1);<ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup><ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup><ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup><ItemGroup>
  122.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  123. </ItemGroup>}<ItemGroup>
  124.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  125. </ItemGroup><ItemGroup>
  126.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  127. </ItemGroup><ItemGroup>
  128.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  129. </ItemGroup><ItemGroup>
  130.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  131. </ItemGroup><ItemGroup>
  132.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  133. </ItemGroup><ItemGroup>
  134.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  135. </ItemGroup>return result;<ItemGroup>
  136.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  137. </ItemGroup><ItemGroup>
  138.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  139. </ItemGroup><ItemGroup>
  140.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  141. </ItemGroup><ItemGroup>
  142.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  143. </ItemGroup>}<ItemGroup>
  144.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  145. </ItemGroup><ItemGroup>
  146.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  147. </ItemGroup>}}
复制代码
gRpcRequest.cs
  1. using Custom.Service;using Grpc.Core;using Grpc.Net.Client;using GrpcService;using static Custom.Service.CustomGreeter;namespace gRpcConsoleTest{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public static class gRpcRequest<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public static async Task SelfIncreaseClient()<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>{<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>using (var channel = GrpcChannel.ForAddress("https://localhost:7166"))<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup>{<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup><ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>CustomGreeterClient client = new CustomGreeterClient(channel);<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>var batch = client.SelfIncreaseClient();<ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup>for (int i = 0; i < 10; i++)<ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup><ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup>{<ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup><ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup><ItemGroup>
  122.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  123. </ItemGroup><ItemGroup>
  124.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  125. </ItemGroup><ItemGroup>
  126.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  127. </ItemGroup><ItemGroup>
  128.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  129. </ItemGroup><ItemGroup>
  130.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  131. </ItemGroup><ItemGroup>
  132.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  133. </ItemGroup>await batch.RequestStream.WriteAsync(new BathTheCatReq() { Id = i });<ItemGroup>
  134.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  135. </ItemGroup><ItemGroup>
  136.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  137. </ItemGroup><ItemGroup>
  138.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  139. </ItemGroup><ItemGroup>
  140.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  141. </ItemGroup><ItemGroup>
  142.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  143. </ItemGroup><ItemGroup>
  144.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  145. </ItemGroup><ItemGroup>
  146.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  147. </ItemGroup><ItemGroup>
  148.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  149. </ItemGroup><ItemGroup>
  150.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  151. </ItemGroup><ItemGroup>
  152.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  153. </ItemGroup>await Task.Delay(1000);<ItemGroup>
  154.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  155. </ItemGroup><ItemGroup>
  156.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  157. </ItemGroup><ItemGroup>
  158.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  159. </ItemGroup><ItemGroup>
  160.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  161. </ItemGroup><ItemGroup>
  162.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  163. </ItemGroup><ItemGroup>
  164.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  165. </ItemGroup><ItemGroup>
  166.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  167. </ItemGroup><ItemGroup>
  168.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  169. </ItemGroup>}<ItemGroup>
  170.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  171. </ItemGroup><ItemGroup>
  172.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  173. </ItemGroup><ItemGroup>
  174.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  175. </ItemGroup><ItemGroup>
  176.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  177. </ItemGroup><ItemGroup>
  178.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  179. </ItemGroup><ItemGroup>
  180.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  181. </ItemGroup><ItemGroup>
  182.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  183. </ItemGroup><ItemGroup>
  184.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  185. </ItemGroup>await batch.RequestStream.CompleteAsync();<ItemGroup>
  186.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  187. </ItemGroup><ItemGroup>
  188.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  189. </ItemGroup><ItemGroup>
  190.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  191. </ItemGroup><ItemGroup>
  192.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  193. </ItemGroup><ItemGroup>
  194.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  195. </ItemGroup><ItemGroup>
  196.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  197. </ItemGroup><ItemGroup>
  198.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  199. </ItemGroup><ItemGroup>
  200.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  201. </ItemGroup>foreach (var item in batch.ResponseAsync.Result.Number)<ItemGroup>
  202.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  203. </ItemGroup><ItemGroup>
  204.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  205. </ItemGroup><ItemGroup>
  206.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  207. </ItemGroup><ItemGroup>
  208.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  209. </ItemGroup><ItemGroup>
  210.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  211. </ItemGroup><ItemGroup>
  212.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  213. </ItemGroup><ItemGroup>
  214.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  215. </ItemGroup><ItemGroup>
  216.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  217. </ItemGroup>{<ItemGroup>
  218.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  219. </ItemGroup><ItemGroup>
  220.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  221. </ItemGroup><ItemGroup>
  222.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  223. </ItemGroup><ItemGroup>
  224.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  225. </ItemGroup><ItemGroup>
  226.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  227. </ItemGroup><ItemGroup>
  228.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  229. </ItemGroup><ItemGroup>
  230.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  231. </ItemGroup><ItemGroup>
  232.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  233. </ItemGroup><ItemGroup>
  234.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  235. </ItemGroup><ItemGroup>
  236.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  237. </ItemGroup>Console.WriteLine($"响应数据: {item}");<ItemGroup>
  238.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  239. </ItemGroup><ItemGroup>
  240.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  241. </ItemGroup><ItemGroup>
  242.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  243. </ItemGroup><ItemGroup>
  244.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  245. </ItemGroup><ItemGroup>
  246.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  247. </ItemGroup><ItemGroup>
  248.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  249. </ItemGroup><ItemGroup>
  250.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  251. </ItemGroup><ItemGroup>
  252.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  253. </ItemGroup>}<ItemGroup>
  254.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  255. </ItemGroup><ItemGroup>
  256.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  257. </ItemGroup><ItemGroup>
  258.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  259. </ItemGroup><ItemGroup>
  260.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  261. </ItemGroup><ItemGroup>
  262.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  263. </ItemGroup><ItemGroup>
  264.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  265. </ItemGroup>}<ItemGroup>
  266.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  267. </ItemGroup><ItemGroup>
  268.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  269. </ItemGroup><ItemGroup>
  270.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  271. </ItemGroup><ItemGroup>
  272.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  273. </ItemGroup>}<ItemGroup>
  274.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  275. </ItemGroup><ItemGroup>
  276.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  277. </ItemGroup>}}
复制代码
双向流式处理方法

custom.proto
  1. syntax = "proto3";option csharp_namespace = "Custom.Service";package custom;service CustomGreeter {<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup>rpc SelfIncreaseDouble(stream BathTheCatReq) returns (stream BathTheCatResp);//双端流}message BathTheCatReq{<ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup><ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup>int32 id = 1;}message BathTheCatResp{<ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup><ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup>string message = 1;}
复制代码
CustomGreeterService.cs
  1. using Grpc.Core;namespace Custom.Service{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class CustomGreeterService : CustomGreeter.CustomGreeterBase<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public override async Task SelfIncreaseDouble(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, ServerCallContext context)<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>{<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>while (await requestStream.MoveNext())<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup>{<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup><ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>var message = requestStream.Current.Id;<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>Console.WriteLine($"客户端流传入消息: {message}");<ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup>await responseStream.WriteAsync(new BathTheCatResp() { Message=(message+1).ToString()});<ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup>}<ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup><ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup>}<ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup>}}
复制代码
gRpcRequest.cs
  1. using Custom.Service;using Grpc.Core;using Grpc.Net.Client;using GrpcService;using static Custom.Service.CustomGreeter;using static GrpcService.Greeter;namespace gRpcConsoleTest{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public static class gRpcRequest<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public static async Task SelfIncreaseDouble()<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>{<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>using (var channel = GrpcChannel.ForAddress("https://localhost:7166"))<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup>{<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup><ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>CustomGreeterClient client = new CustomGreeterClient(channel);<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>var batch = client.SelfIncreaseDouble();<ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup>Task batchTask = Task.Run(async () =><ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup><ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup>{<ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup><ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup><ItemGroup>
  122.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  123. </ItemGroup><ItemGroup>
  124.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  125. </ItemGroup><ItemGroup>
  126.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  127. </ItemGroup><ItemGroup>
  128.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  129. </ItemGroup><ItemGroup>
  130.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  131. </ItemGroup><ItemGroup>
  132.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  133. </ItemGroup>await foreach (var item in batch.ResponseStream.ReadAllAsync())<ItemGroup>
  134.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  135. </ItemGroup><ItemGroup>
  136.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  137. </ItemGroup><ItemGroup>
  138.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  139. </ItemGroup><ItemGroup>
  140.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  141. </ItemGroup><ItemGroup>
  142.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  143. </ItemGroup><ItemGroup>
  144.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  145. </ItemGroup><ItemGroup>
  146.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  147. </ItemGroup><ItemGroup>
  148.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  149. </ItemGroup><ItemGroup>
  150.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  151. </ItemGroup><ItemGroup>
  152.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  153. </ItemGroup>{<ItemGroup>
  154.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  155. </ItemGroup><ItemGroup>
  156.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  157. </ItemGroup><ItemGroup>
  158.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  159. </ItemGroup><ItemGroup>
  160.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  161. </ItemGroup><ItemGroup>
  162.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  163. </ItemGroup><ItemGroup>
  164.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  165. </ItemGroup><ItemGroup>
  166.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  167. </ItemGroup><ItemGroup>
  168.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  169. </ItemGroup><ItemGroup>
  170.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  171. </ItemGroup><ItemGroup>
  172.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  173. </ItemGroup><ItemGroup>
  174.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  175. </ItemGroup><ItemGroup>
  176.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  177. </ItemGroup>Console.WriteLine($"服务端相应数据: {item.Message}");<ItemGroup>
  178.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  179. </ItemGroup><ItemGroup>
  180.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  181. </ItemGroup><ItemGroup>
  182.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  183. </ItemGroup><ItemGroup>
  184.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  185. </ItemGroup><ItemGroup>
  186.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  187. </ItemGroup><ItemGroup>
  188.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  189. </ItemGroup><ItemGroup>
  190.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  191. </ItemGroup><ItemGroup>
  192.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  193. </ItemGroup><ItemGroup>
  194.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  195. </ItemGroup><ItemGroup>
  196.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  197. </ItemGroup>}<ItemGroup>
  198.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  199. </ItemGroup><ItemGroup>
  200.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  201. </ItemGroup><ItemGroup>
  202.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  203. </ItemGroup><ItemGroup>
  204.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  205. </ItemGroup><ItemGroup>
  206.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  207. </ItemGroup><ItemGroup>
  208.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  209. </ItemGroup><ItemGroup>
  210.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  211. </ItemGroup><ItemGroup>
  212.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  213. </ItemGroup>});<ItemGroup>
  214.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  215. </ItemGroup><ItemGroup>
  216.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  217. </ItemGroup><ItemGroup>
  218.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  219. </ItemGroup><ItemGroup>
  220.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  221. </ItemGroup><ItemGroup>
  222.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  223. </ItemGroup><ItemGroup>
  224.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  225. </ItemGroup><ItemGroup>
  226.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  227. </ItemGroup><ItemGroup>
  228.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  229. </ItemGroup>for (int i = 0; i < 10; i++)<ItemGroup>
  230.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  231. </ItemGroup><ItemGroup>
  232.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  233. </ItemGroup><ItemGroup>
  234.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  235. </ItemGroup><ItemGroup>
  236.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  237. </ItemGroup><ItemGroup>
  238.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  239. </ItemGroup><ItemGroup>
  240.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  241. </ItemGroup><ItemGroup>
  242.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  243. </ItemGroup><ItemGroup>
  244.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  245. </ItemGroup>{<ItemGroup>
  246.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  247. </ItemGroup><ItemGroup>
  248.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  249. </ItemGroup><ItemGroup>
  250.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  251. </ItemGroup><ItemGroup>
  252.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  253. </ItemGroup><ItemGroup>
  254.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  255. </ItemGroup><ItemGroup>
  256.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  257. </ItemGroup><ItemGroup>
  258.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  259. </ItemGroup><ItemGroup>
  260.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  261. </ItemGroup><ItemGroup>
  262.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  263. </ItemGroup><ItemGroup>
  264.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  265. </ItemGroup>await batch.RequestStream.WriteAsync(new BathTheCatReq() { Id = i });<ItemGroup>
  266.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  267. </ItemGroup><ItemGroup>
  268.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  269. </ItemGroup><ItemGroup>
  270.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  271. </ItemGroup><ItemGroup>
  272.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  273. </ItemGroup><ItemGroup>
  274.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  275. </ItemGroup><ItemGroup>
  276.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  277. </ItemGroup><ItemGroup>
  278.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  279. </ItemGroup><ItemGroup>
  280.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  281. </ItemGroup><ItemGroup>
  282.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  283. </ItemGroup><ItemGroup>
  284.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  285. </ItemGroup>await Task.Delay(1000);<ItemGroup>
  286.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  287. </ItemGroup><ItemGroup>
  288.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  289. </ItemGroup><ItemGroup>
  290.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  291. </ItemGroup><ItemGroup>
  292.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  293. </ItemGroup><ItemGroup>
  294.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  295. </ItemGroup><ItemGroup>
  296.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  297. </ItemGroup><ItemGroup>
  298.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  299. </ItemGroup><ItemGroup>
  300.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  301. </ItemGroup>}<ItemGroup>
  302.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  303. </ItemGroup><ItemGroup>
  304.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  305. </ItemGroup><ItemGroup>
  306.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  307. </ItemGroup><ItemGroup>
  308.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  309. </ItemGroup><ItemGroup>
  310.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  311. </ItemGroup><ItemGroup>
  312.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  313. </ItemGroup><ItemGroup>
  314.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  315. </ItemGroup><ItemGroup>
  316.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  317. </ItemGroup>await batchTask;<ItemGroup>
  318.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  319. </ItemGroup><ItemGroup>
  320.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  321. </ItemGroup><ItemGroup>
  322.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  323. </ItemGroup><ItemGroup>
  324.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  325. </ItemGroup><ItemGroup>
  326.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  327. </ItemGroup><ItemGroup>
  328.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  329. </ItemGroup>}<ItemGroup>
  330.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  331. </ItemGroup><ItemGroup>
  332.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  333. </ItemGroup><ItemGroup>
  334.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  335. </ItemGroup><ItemGroup>
  336.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  337. </ItemGroup>}<ItemGroup>
  338.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  339. </ItemGroup><ItemGroup>
  340.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  341. </ItemGroup>}}
复制代码
.Net Core 调用gRpc

项目引用
  1. <ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>
复制代码
Program.cs
  1. //CustomGreeterClient grpc连接类builder.Services.AddGrpcClient(options =>{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>options.Address = new Uri("https://localhost:7166"); //grpc 服务地址});
复制代码
gRpcController.cs
  1. using Custom.Service;using Microsoft.AspNetCore.Mvc;using static Custom.Service.CustomGreeter;namespace gRpcWebAPI.Controllers{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>[Route("api/[controller]/[action]")]<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>[ApiController]<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup>public class gRpcController : ControllerBase<ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>{<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>CustomGreeterClient _client; //使用构造函数注入<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup>public gRpcController(CustomGreeterClient client)<ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup><ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup>{<ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup><ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup>_client = client;<ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup>}<ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup><ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup>[HttpGet]<ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup>public async Task Plus(int leftNumber, int rightNumber)<ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup><ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup>{<ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup>NumberResult number = await _client.PlusAsync(new Custom.Service.Number() { LeftNumber = leftNumber, RightNumber = rightNumber });<ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup>return new JsonResult(number);<ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup><ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup>}<ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup>}}
复制代码
支持Aop


  • 服务端, 客户端 都需要继承 Interceptor
  • 重新需要实现Aop的方法,如服务端: UnaryServerHandler,客户端: AsyncUnaryCall 针对一元调用的Aop
  • 我这里使用的是NLog写的日志,NLog可以使用可以翻阅我先前的博客
    .Net Core NLog+oracel
服务端 Program.cs
  1. builder.Services.AddGrpc(options =>{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>options.Interceptors.Add();});
复制代码
服务端 LogInterceptor.cs
  1. using Grpc.Core;using Grpc.Core.Interceptors;namespace GrpcService.Interceptors{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class LogInterceptor : Interceptor<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>ILogger _logger;<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>public LogInterceptor(ILogger logger)<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup>{<ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup><ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup>_logger = logger;<ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup><ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup>}<ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup>public override Task UnaryServerHandler(TRequest request, ServerCallContext context, UnaryServerMethod continuation)<ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup><ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup>{<ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>_logger.LogInformation("===========UnaryServerHandler==========");<ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup>return continuation(request, context);<ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup><ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup>}<ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup>}}
复制代码
客户端 Program.cs
  1. builder.Services.AddGrpcClient(options =>{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>options.Address = new Uri("https://localhost:7166"); //服务端地址}).AddInterceptor();
复制代码
客户端 LogInterceptor.cs
  1. using Grpc.Core;using Grpc.Core.Interceptors;namespace GrpcService.Interceptors{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class LogInterceptor : Interceptor<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>ILogger _logger;<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>public LogInterceptor(ILogger logger)<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup>{<ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup><ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup>_logger = logger;<ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup><ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup>}<ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup>public override AsyncUnaryCall AsyncUnaryCall(TRequest request, ClientInterceptorContext context, AsyncUnaryCallContinuation continuation)<ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup><ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup>{<ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>_logger.LogInformation("===========AsyncUnaryCall===========");<ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup>return continuation(request, context);<ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup><ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup>}<ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup>}}
复制代码
jwt+gRPC验证


  • 准备单独的一个网站发布jwt Token (授权中心)
  • 然后在gRPC项目中jwt鉴权 和 获取到角色信息之后授权
  • webapi测试gRPC调用, 先在授权中心获取token然后在 ConfigureChannel 方法中设置gRPC全局的jwt token
  • 返回401: jwt鉴权不通过
  • 返回403: jwt授权不通过
准备Jwt Token发布中心

Program.cs
  1. //读取Jwt配置
  2. builder.Services.Configure<JwtConfig>(builder.Configuration.GetSection("JwtTokenOptions"));
复制代码
JwtConfig.cs
  1. namespace AuthenorizationCenter.Tools.Model{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class JwtConfig<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public string? Audience { get; set; }<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>public string? Issuer { get; set; }<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup>public string? SecurityKey { get; set; }<ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup><ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup>public int ExpiresMinutes { get; set; }<ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup>}}
复制代码
AuthenorizationController.cs
  1. using AuthenorizationCenter.Tools;using AuthenorizationCenter.Tools.Model;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Options;namespace AuthenorizationCenter.Controllers{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>[Route("api/[controller]")]<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>[ApiController]<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup>public class AuthenorizationController : ControllerBase<ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>{<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>JwtConfig _jwtconfig;<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup>public AuthenorizationController(IOptions jwtconfig)<ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup><ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup> {<ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup><ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup>_jwtconfig = jwtconfig.Value;<ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup>}<ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup><ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup>[HttpGet]<ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup>public async Task GetToken(string userName, string passWord)<ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup><ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup>{<ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup>string token = JwtHeleper.GetToken(new()<ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup>{<ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup><ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup><ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup><ItemGroup>
  122.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  123. </ItemGroup><ItemGroup>
  124.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  125. </ItemGroup>UserName = userName,<ItemGroup>
  126.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  127. </ItemGroup><ItemGroup>
  128.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  129. </ItemGroup><ItemGroup>
  130.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  131. </ItemGroup><ItemGroup>
  132.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  133. </ItemGroup><ItemGroup>
  134.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  135. </ItemGroup><ItemGroup>
  136.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  137. </ItemGroup><ItemGroup>
  138.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  139. </ItemGroup><ItemGroup>
  140.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  141. </ItemGroup>Extended1 = "无信息",<ItemGroup>
  142.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  143. </ItemGroup><ItemGroup>
  144.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  145. </ItemGroup><ItemGroup>
  146.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  147. </ItemGroup><ItemGroup>
  148.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  149. </ItemGroup><ItemGroup>
  150.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  151. </ItemGroup><ItemGroup>
  152.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  153. </ItemGroup><ItemGroup>
  154.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  155. </ItemGroup><ItemGroup>
  156.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  157. </ItemGroup>Role = new List()<ItemGroup>
  158.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  159. </ItemGroup><ItemGroup>
  160.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  161. </ItemGroup><ItemGroup>
  162.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  163. </ItemGroup><ItemGroup>
  164.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  165. </ItemGroup><ItemGroup>
  166.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  167. </ItemGroup><ItemGroup>
  168.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  169. </ItemGroup><ItemGroup>
  170.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  171. </ItemGroup><ItemGroup>
  172.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  173. </ItemGroup>{<ItemGroup>
  174.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  175. </ItemGroup><ItemGroup>
  176.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  177. </ItemGroup><ItemGroup>
  178.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  179. </ItemGroup><ItemGroup>
  180.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  181. </ItemGroup><ItemGroup>
  182.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  183. </ItemGroup><ItemGroup>
  184.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  185. </ItemGroup><ItemGroup>
  186.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  187. </ItemGroup><ItemGroup>
  188.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  189. </ItemGroup><ItemGroup>
  190.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  191. </ItemGroup><ItemGroup>
  192.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  193. </ItemGroup>new RoleInfo() { Id = "1",Role="系统管理员"} ,<ItemGroup>
  194.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  195. </ItemGroup><ItemGroup>
  196.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  197. </ItemGroup><ItemGroup>
  198.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  199. </ItemGroup><ItemGroup>
  200.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  201. </ItemGroup><ItemGroup>
  202.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  203. </ItemGroup><ItemGroup>
  204.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  205. </ItemGroup><ItemGroup>
  206.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  207. </ItemGroup><ItemGroup>
  208.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  209. </ItemGroup><ItemGroup>
  210.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  211. </ItemGroup><ItemGroup>
  212.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  213. </ItemGroup>new RoleInfo() { Id = "2",Role="用户管理员"} ,<ItemGroup>
  214.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  215. </ItemGroup><ItemGroup>
  216.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  217. </ItemGroup><ItemGroup>
  218.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  219. </ItemGroup><ItemGroup>
  220.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  221. </ItemGroup><ItemGroup>
  222.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  223. </ItemGroup><ItemGroup>
  224.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  225. </ItemGroup><ItemGroup>
  226.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  227. </ItemGroup><ItemGroup>
  228.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  229. </ItemGroup>}<ItemGroup>
  230.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  231. </ItemGroup><ItemGroup>
  232.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  233. </ItemGroup><ItemGroup>
  234.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  235. </ItemGroup><ItemGroup>
  236.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  237. </ItemGroup><ItemGroup>
  238.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  239. </ItemGroup><ItemGroup>
  240.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  241. </ItemGroup>}, new()<ItemGroup>
  242.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  243. </ItemGroup><ItemGroup>
  244.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  245. </ItemGroup><ItemGroup>
  246.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  247. </ItemGroup><ItemGroup>
  248.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  249. </ItemGroup><ItemGroup>
  250.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  251. </ItemGroup><ItemGroup>
  252.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  253. </ItemGroup>{<ItemGroup>
  254.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  255. </ItemGroup><ItemGroup>
  256.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  257. </ItemGroup><ItemGroup>
  258.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  259. </ItemGroup><ItemGroup>
  260.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  261. </ItemGroup><ItemGroup>
  262.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  263. </ItemGroup><ItemGroup>
  264.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  265. </ItemGroup><ItemGroup>
  266.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  267. </ItemGroup><ItemGroup>
  268.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  269. </ItemGroup>Audience = _jwtconfig.Audience,<ItemGroup>
  270.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  271. </ItemGroup><ItemGroup>
  272.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  273. </ItemGroup><ItemGroup>
  274.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  275. </ItemGroup><ItemGroup>
  276.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  277. </ItemGroup><ItemGroup>
  278.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  279. </ItemGroup><ItemGroup>
  280.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  281. </ItemGroup><ItemGroup>
  282.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  283. </ItemGroup><ItemGroup>
  284.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  285. </ItemGroup>Issuer = _jwtconfig.Issuer,<ItemGroup>
  286.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  287. </ItemGroup><ItemGroup>
  288.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  289. </ItemGroup><ItemGroup>
  290.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  291. </ItemGroup><ItemGroup>
  292.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  293. </ItemGroup><ItemGroup>
  294.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  295. </ItemGroup><ItemGroup>
  296.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  297. </ItemGroup><ItemGroup>
  298.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  299. </ItemGroup><ItemGroup>
  300.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  301. </ItemGroup>SecurityKey = _jwtconfig.SecurityKey,<ItemGroup>
  302.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  303. </ItemGroup><ItemGroup>
  304.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  305. </ItemGroup><ItemGroup>
  306.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  307. </ItemGroup><ItemGroup>
  308.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  309. </ItemGroup><ItemGroup>
  310.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  311. </ItemGroup><ItemGroup>
  312.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  313. </ItemGroup><ItemGroup>
  314.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  315. </ItemGroup><ItemGroup>
  316.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  317. </ItemGroup>ExpiresMinutes = 5,<ItemGroup>
  318.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  319. </ItemGroup><ItemGroup>
  320.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  321. </ItemGroup><ItemGroup>
  322.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  323. </ItemGroup><ItemGroup>
  324.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  325. </ItemGroup><ItemGroup>
  326.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  327. </ItemGroup><ItemGroup>
  328.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  329. </ItemGroup>});<ItemGroup>
  330.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  331. </ItemGroup><ItemGroup>
  332.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  333. </ItemGroup><ItemGroup>
  334.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  335. </ItemGroup><ItemGroup>
  336.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  337. </ItemGroup><ItemGroup>
  338.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  339. </ItemGroup><ItemGroup>
  340.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  341. </ItemGroup>await Task.CompletedTask;<ItemGroup>
  342.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  343. </ItemGroup><ItemGroup>
  344.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  345. </ItemGroup><ItemGroup>
  346.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  347. </ItemGroup><ItemGroup>
  348.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  349. </ItemGroup><ItemGroup>
  350.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  351. </ItemGroup><ItemGroup>
  352.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  353. </ItemGroup>return token;<ItemGroup>
  354.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  355. </ItemGroup><ItemGroup>
  356.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  357. </ItemGroup><ItemGroup>
  358.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  359. </ItemGroup><ItemGroup>
  360.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  361. </ItemGroup>}<ItemGroup>
  362.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  363. </ItemGroup><ItemGroup>
  364.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  365. </ItemGroup>}}
复制代码
JwtHeleper.cs
  1. using AuthenorizationCenter.Tools.Model;using Microsoft.IdentityModel.Tokens;using System.IdentityModel.Tokens.Jwt;using System.Security.Claims;using System.Text;namespace AuthenorizationCenter.Tools{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class JwtHeleper<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public static string GetToken(UserInfo user, JwtConfig jwtConfig)<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>{<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>List claims = new List<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup>{<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup><ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>new Claim(ClaimTypes.Name, user.UserName ?? ""),<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>new Claim("Extended1", user.Extended1 ?? ""),<ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup>new Claim("Extended2", user.Extended2 ?? ""),<ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup><ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup>new Claim("Extended3", user.Extended3 ?? ""),<ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup><ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup><ItemGroup>
  122.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  123. </ItemGroup><ItemGroup>
  124.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  125. </ItemGroup><ItemGroup>
  126.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  127. </ItemGroup><ItemGroup>
  128.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  129. </ItemGroup>new Claim("Extended4", user.Extended4 ?? ""),<ItemGroup>
  130.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  131. </ItemGroup><ItemGroup>
  132.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  133. </ItemGroup><ItemGroup>
  134.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  135. </ItemGroup><ItemGroup>
  136.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  137. </ItemGroup><ItemGroup>
  138.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  139. </ItemGroup><ItemGroup>
  140.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  141. </ItemGroup><ItemGroup>
  142.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  143. </ItemGroup><ItemGroup>
  144.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  145. </ItemGroup>new Claim("Extended5", user.Extended5 ?? ""),<ItemGroup>
  146.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  147. </ItemGroup><ItemGroup>
  148.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  149. </ItemGroup><ItemGroup>
  150.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  151. </ItemGroup><ItemGroup>
  152.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  153. </ItemGroup><ItemGroup>
  154.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  155. </ItemGroup><ItemGroup>
  156.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  157. </ItemGroup>};<ItemGroup>
  158.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  159. </ItemGroup><ItemGroup>
  160.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  161. </ItemGroup><ItemGroup>
  162.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  163. </ItemGroup><ItemGroup>
  164.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  165. </ItemGroup><ItemGroup>
  166.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  167. </ItemGroup><ItemGroup>
  168.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  169. </ItemGroup>if (user.Role is not null)<ItemGroup>
  170.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  171. </ItemGroup><ItemGroup>
  172.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  173. </ItemGroup><ItemGroup>
  174.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  175. </ItemGroup><ItemGroup>
  176.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  177. </ItemGroup><ItemGroup>
  178.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  179. </ItemGroup><ItemGroup>
  180.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  181. </ItemGroup>{<ItemGroup>
  182.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  183. </ItemGroup><ItemGroup>
  184.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  185. </ItemGroup><ItemGroup>
  186.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  187. </ItemGroup><ItemGroup>
  188.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  189. </ItemGroup><ItemGroup>
  190.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  191. </ItemGroup><ItemGroup>
  192.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  193. </ItemGroup><ItemGroup>
  194.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  195. </ItemGroup><ItemGroup>
  196.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  197. </ItemGroup>foreach (var item in user.Role)<ItemGroup>
  198.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  199. </ItemGroup><ItemGroup>
  200.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  201. </ItemGroup><ItemGroup>
  202.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  203. </ItemGroup><ItemGroup>
  204.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  205. </ItemGroup><ItemGroup>
  206.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  207. </ItemGroup><ItemGroup>
  208.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  209. </ItemGroup><ItemGroup>
  210.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  211. </ItemGroup><ItemGroup>
  212.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  213. </ItemGroup>{<ItemGroup>
  214.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  215. </ItemGroup><ItemGroup>
  216.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  217. </ItemGroup><ItemGroup>
  218.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  219. </ItemGroup><ItemGroup>
  220.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  221. </ItemGroup><ItemGroup>
  222.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  223. </ItemGroup><ItemGroup>
  224.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  225. </ItemGroup><ItemGroup>
  226.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  227. </ItemGroup><ItemGroup>
  228.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  229. </ItemGroup><ItemGroup>
  230.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  231. </ItemGroup><ItemGroup>
  232.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  233. </ItemGroup>claims.Add(new Claim(item.Id.ToString(), item.Role));<ItemGroup>
  234.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  235. </ItemGroup><ItemGroup>
  236.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  237. </ItemGroup><ItemGroup>
  238.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  239. </ItemGroup><ItemGroup>
  240.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  241. </ItemGroup><ItemGroup>
  242.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  243. </ItemGroup><ItemGroup>
  244.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  245. </ItemGroup><ItemGroup>
  246.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  247. </ItemGroup><ItemGroup>
  248.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  249. </ItemGroup>}<ItemGroup>
  250.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  251. </ItemGroup><ItemGroup>
  252.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  253. </ItemGroup><ItemGroup>
  254.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  255. </ItemGroup><ItemGroup>
  256.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  257. </ItemGroup><ItemGroup>
  258.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  259. </ItemGroup><ItemGroup>
  260.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  261. </ItemGroup>}<ItemGroup>
  262.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  263. </ItemGroup><ItemGroup>
  264.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  265. </ItemGroup><ItemGroup>
  266.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  267. </ItemGroup><ItemGroup>
  268.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  269. </ItemGroup><ItemGroup>
  270.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  271. </ItemGroup><ItemGroup>
  272.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  273. </ItemGroup>if (jwtConfig.SecurityKey == null)<ItemGroup>
  274.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  275. </ItemGroup><ItemGroup>
  276.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  277. </ItemGroup><ItemGroup>
  278.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  279. </ItemGroup><ItemGroup>
  280.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  281. </ItemGroup><ItemGroup>
  282.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  283. </ItemGroup><ItemGroup>
  284.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  285. </ItemGroup>{<ItemGroup>
  286.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  287. </ItemGroup><ItemGroup>
  288.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  289. </ItemGroup><ItemGroup>
  290.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  291. </ItemGroup><ItemGroup>
  292.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  293. </ItemGroup><ItemGroup>
  294.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  295. </ItemGroup><ItemGroup>
  296.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  297. </ItemGroup><ItemGroup>
  298.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  299. </ItemGroup><ItemGroup>
  300.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  301. </ItemGroup>throw new Exception("JwtConfig.SecurityKey 不能为空");<ItemGroup>
  302.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  303. </ItemGroup><ItemGroup>
  304.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  305. </ItemGroup><ItemGroup>
  306.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  307. </ItemGroup><ItemGroup>
  308.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  309. </ItemGroup><ItemGroup>
  310.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  311. </ItemGroup><ItemGroup>
  312.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  313. </ItemGroup>}<ItemGroup>
  314.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  315. </ItemGroup><ItemGroup>
  316.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  317. </ItemGroup><ItemGroup>
  318.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  319. </ItemGroup><ItemGroup>
  320.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  321. </ItemGroup><ItemGroup>
  322.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  323. </ItemGroup><ItemGroup>
  324.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  325. </ItemGroup>SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConfig.SecurityKey));<ItemGroup>
  326.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  327. </ItemGroup><ItemGroup>
  328.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  329. </ItemGroup><ItemGroup>
  330.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  331. </ItemGroup><ItemGroup>
  332.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  333. </ItemGroup><ItemGroup>
  334.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  335. </ItemGroup><ItemGroup>
  336.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  337. </ItemGroup>SigningCredentials creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);<ItemGroup>
  338.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  339. </ItemGroup><ItemGroup>
  340.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  341. </ItemGroup><ItemGroup>
  342.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  343. </ItemGroup><ItemGroup>
  344.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  345. </ItemGroup><ItemGroup>
  346.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  347. </ItemGroup><ItemGroup>
  348.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  349. </ItemGroup>JwtSecurityToken token = new JwtSecurityToken(<ItemGroup>
  350.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  351. </ItemGroup><ItemGroup>
  352.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  353. </ItemGroup><ItemGroup>
  354.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  355. </ItemGroup><ItemGroup>
  356.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  357. </ItemGroup><ItemGroup>
  358.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  359. </ItemGroup><ItemGroup>
  360.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  361. </ItemGroup><ItemGroup>
  362.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  363. </ItemGroup><ItemGroup>
  364.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  365. </ItemGroup>issuer: jwtConfig.Issuer,<ItemGroup>
  366.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  367. </ItemGroup><ItemGroup>
  368.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  369. </ItemGroup><ItemGroup>
  370.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  371. </ItemGroup><ItemGroup>
  372.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  373. </ItemGroup><ItemGroup>
  374.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  375. </ItemGroup><ItemGroup>
  376.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  377. </ItemGroup><ItemGroup>
  378.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  379. </ItemGroup><ItemGroup>
  380.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  381. </ItemGroup>audience: jwtConfig.Audience,<ItemGroup>
  382.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  383. </ItemGroup><ItemGroup>
  384.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  385. </ItemGroup><ItemGroup>
  386.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  387. </ItemGroup><ItemGroup>
  388.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  389. </ItemGroup><ItemGroup>
  390.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  391. </ItemGroup><ItemGroup>
  392.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  393. </ItemGroup><ItemGroup>
  394.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  395. </ItemGroup><ItemGroup>
  396.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  397. </ItemGroup>claims: claims,<ItemGroup>
  398.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  399. </ItemGroup><ItemGroup>
  400.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  401. </ItemGroup><ItemGroup>
  402.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  403. </ItemGroup><ItemGroup>
  404.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  405. </ItemGroup><ItemGroup>
  406.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  407. </ItemGroup><ItemGroup>
  408.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  409. </ItemGroup><ItemGroup>
  410.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  411. </ItemGroup><ItemGroup>
  412.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  413. </ItemGroup>expires: DateTime.UtcNow.AddMinutes(jwtConfig.ExpiresMinutes),<ItemGroup>
  414.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  415. </ItemGroup><ItemGroup>
  416.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  417. </ItemGroup><ItemGroup>
  418.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  419. </ItemGroup><ItemGroup>
  420.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  421. </ItemGroup><ItemGroup>
  422.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  423. </ItemGroup><ItemGroup>
  424.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  425. </ItemGroup><ItemGroup>
  426.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  427. </ItemGroup><ItemGroup>
  428.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  429. </ItemGroup>signingCredentials: creds<ItemGroup>
  430.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  431. </ItemGroup><ItemGroup>
  432.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  433. </ItemGroup><ItemGroup>
  434.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  435. </ItemGroup><ItemGroup>
  436.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  437. </ItemGroup><ItemGroup>
  438.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  439. </ItemGroup><ItemGroup>
  440.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  441. </ItemGroup>);<ItemGroup>
  442.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  443. </ItemGroup><ItemGroup>
  444.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  445. </ItemGroup><ItemGroup>
  446.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  447. </ItemGroup><ItemGroup>
  448.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  449. </ItemGroup><ItemGroup>
  450.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  451. </ItemGroup><ItemGroup>
  452.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  453. </ItemGroup>string resultToken = new JwtSecurityTokenHandler().WriteToken(token);<ItemGroup>
  454.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  455. </ItemGroup><ItemGroup>
  456.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  457. </ItemGroup><ItemGroup>
  458.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  459. </ItemGroup><ItemGroup>
  460.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  461. </ItemGroup><ItemGroup>
  462.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  463. </ItemGroup><ItemGroup>
  464.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  465. </ItemGroup>return resultToken;<ItemGroup>
  466.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  467. </ItemGroup><ItemGroup>
  468.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  469. </ItemGroup><ItemGroup>
  470.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  471. </ItemGroup><ItemGroup>
  472.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  473. </ItemGroup>}<ItemGroup>
  474.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  475. </ItemGroup><ItemGroup>
  476.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  477. </ItemGroup>}}
复制代码
RoleInfo.cs
  1. namespace AuthenorizationCenter.Tools.Model{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class RoleInfo<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public string Id { get; set; }<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>public string Role { get; set; }<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup>}}
复制代码
UserInfo.cs
  1. namespace AuthenorizationCenter.Tools.Model{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class UserInfo<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public string? UserName { get; set; }<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>public List? Role { get; set; }<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup>public string? Extended1 { get; set; }<ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup><ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup>public string? Extended2 { get; set; }<ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup>public string? Extended3 { get; set; }<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup>public string? Extended4 { get; set; }<ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup><ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>public string? Extended5 { get; set; }<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup>}}
复制代码
appsetting.json
  1. {<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup>"JwtTokenOptions": {<ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup><ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup>"Issuer": "https://localhost:7117",<ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup><ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup>"Audience": "https://localhost:7117",<ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup>"SecurityKey": "kq4DY5N1eFJhscOkI7Zp4Nd0WNy9d9AEsN6Yjgdv9OxLyol66tzGBKT_7vwolN7GZ8EDwqJBwccjDJfb81ws5s3sbbP5wUzQ3-PcTSsD-Rueiu2rsOUZwg_NR3RBCwmtouV-832YV2trCjNTawLB1z0LMukWGFNaAJVZ8WdQcrYn6a0ko5oVhZqaHBgsCLEGiqPtoFsiCcrJTz1IvXHk9_cDSr2hwEmSl18GlkOtgCHFH8aidYth3aQHRHuClTi6Y9mYRJtqqK-FNQYq4ZP23DSGZGFejJFTnM9YMpppuTMLklhSGySwX8rfjZ_0L5ac18nHaykTaiC2fvH00W42qQ"<ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>}}
复制代码
gRPC准备

Program.cs
  1. JwtConfig jwtConfig = new JwtConfig();builder.Configuration.Bind("JwtTokenOptions", jwtConfig);builder.Services.AddAuthentication(options =>{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;}).AddJwtBearer(options =>{<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>options.TokenValidationParameters = new TokenValidationParameters()<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup>{<ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup><ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup>ValidateIssuer = true,<ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup><ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup>ValidIssuer = jwtConfig.Issuer, //发行人<ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>ValidateAudience = true,<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup>ValidAudience = jwtConfig.Audience,//订阅人<ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup><ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup>ValidateIssuerSigningKey = true,<ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup>//对称加密密钥<ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup><ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup>IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConfig.SecurityKey!)),<ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup>ValidateLifetime = true, //验证失效时间<ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup><ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup>ClockSkew = TimeSpan.FromSeconds(30), //过期时间容错值<ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup>RequireExpirationTime = true,<ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup><ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup>AudienceValidator = (audiences, securityToken, validationParameters) =><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup>{<ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup><ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup><ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup>return true;<ItemGroup>
  122.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  123. </ItemGroup><ItemGroup>
  124.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  125. </ItemGroup><ItemGroup>
  126.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  127. </ItemGroup><ItemGroup>
  128.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  129. </ItemGroup>},<ItemGroup>
  130.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  131. </ItemGroup><ItemGroup>
  132.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  133. </ItemGroup><ItemGroup>
  134.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  135. </ItemGroup><ItemGroup>
  136.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  137. </ItemGroup>LifetimeValidator = (notBefore, expires, securityToken, validationParameters) =><ItemGroup>
  138.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  139. </ItemGroup><ItemGroup>
  140.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  141. </ItemGroup><ItemGroup>
  142.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  143. </ItemGroup><ItemGroup>
  144.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  145. </ItemGroup>{<ItemGroup>
  146.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  147. </ItemGroup><ItemGroup>
  148.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  149. </ItemGroup><ItemGroup>
  150.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  151. </ItemGroup><ItemGroup>
  152.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  153. </ItemGroup><ItemGroup>
  154.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  155. </ItemGroup><ItemGroup>
  156.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  157. </ItemGroup>return true;<ItemGroup>
  158.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  159. </ItemGroup><ItemGroup>
  160.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  161. </ItemGroup><ItemGroup>
  162.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  163. </ItemGroup><ItemGroup>
  164.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  165. </ItemGroup>}<ItemGroup>
  166.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  167. </ItemGroup><ItemGroup>
  168.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  169. </ItemGroup>};});builder.Services.AddTransient();builder.Services.AddTransient();builder.Services.AddAuthorization(options =>{<ItemGroup>
  170.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  171. </ItemGroup><ItemGroup>
  172.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  173. </ItemGroup>options.AddPolicy("JwtPolicy", policy =><ItemGroup>
  174.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  175. </ItemGroup><ItemGroup>
  176.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  177. </ItemGroup>{<ItemGroup>
  178.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  179. </ItemGroup><ItemGroup>
  180.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  181. </ItemGroup><ItemGroup>
  182.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  183. </ItemGroup><ItemGroup>
  184.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  185. </ItemGroup>//jwt 授权<ItemGroup>
  186.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  187. </ItemGroup><ItemGroup>
  188.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  189. </ItemGroup><ItemGroup>
  190.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  191. </ItemGroup><ItemGroup>
  192.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  193. </ItemGroup>policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)<ItemGroup>
  194.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  195. </ItemGroup><ItemGroup>
  196.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  197. </ItemGroup><ItemGroup>
  198.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  199. </ItemGroup><ItemGroup>
  200.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  201. </ItemGroup>//这里为自定义授权指定一下类<ItemGroup>
  202.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  203. </ItemGroup><ItemGroup>
  204.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  205. </ItemGroup><ItemGroup>
  206.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  207. </ItemGroup><ItemGroup>
  208.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  209. </ItemGroup>.AddRequirements(new UserRoleRequirement(JwtBearerDefaults.AuthenticationScheme));<ItemGroup>
  210.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  211. </ItemGroup><ItemGroup>
  212.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  213. </ItemGroup>});});
复制代码
CustomGreeterService.cs
  1. using Grpc.Core;using Microsoft.AspNetCore.Authentication.JwtBearer;using Microsoft.AspNetCore.Authorization;namespace Custom.Service{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class CustomGreeterService : CustomGreeter.CustomGreeterBase<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme,Policy = "JwtPolicy")]<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>public override async Task Plus(Number request, ServerCallContext context) =><ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup> await Task.FromResult(new NumberResult()<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup>{<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup><ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>Result = request.LeftNumber + request.RightNumber,<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup>});<ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>}}
复制代码
JwtAuthorization.cs
  1. using Cnpc.Com.Ioc.IBll;using Microsoft.AspNetCore.Authorization;using System.Security.Claims;namespace GrpcService.Authorization{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public class UserRoleRequirement : IAuthorizationRequirement<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public string AuthenticateScheme;<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>public UserRoleRequirement(string authenticateScheme)<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup>{<ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup><ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup>AuthenticateScheme = authenticateScheme;<ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup><ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup>}<ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup>}<ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup>public class JwtAuthorization : AuthorizationHandler<ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>{<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup>IUserServices userSercices;<ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup>public JwtAuthorization(IUserServices userSercices)<ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup><ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup>{<ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup><ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup>this.userSercices = userSercices;<ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup>}<ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup><ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup>protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserRoleRequirement requirement)<ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup><ItemGroup>
  122.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  123. </ItemGroup><ItemGroup>
  124.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  125. </ItemGroup>{<ItemGroup>
  126.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  127. </ItemGroup><ItemGroup>
  128.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  129. </ItemGroup><ItemGroup>
  130.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  131. </ItemGroup><ItemGroup>
  132.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  133. </ItemGroup><ItemGroup>
  134.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  135. </ItemGroup><ItemGroup>
  136.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  137. </ItemGroup>string? userName = context.User.FindFirst(it => it.Type == ClaimTypes.Name)?.Value;<ItemGroup>
  138.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  139. </ItemGroup><ItemGroup>
  140.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  141. </ItemGroup><ItemGroup>
  142.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  143. </ItemGroup><ItemGroup>
  144.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  145. </ItemGroup><ItemGroup>
  146.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  147. </ItemGroup><ItemGroup>
  148.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  149. </ItemGroup>if (userSercices.IsAdmin(userName!))<ItemGroup>
  150.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  151. </ItemGroup><ItemGroup>
  152.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  153. </ItemGroup><ItemGroup>
  154.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  155. </ItemGroup><ItemGroup>
  156.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  157. </ItemGroup><ItemGroup>
  158.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  159. </ItemGroup><ItemGroup>
  160.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  161. </ItemGroup>{<ItemGroup>
  162.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  163. </ItemGroup><ItemGroup>
  164.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  165. </ItemGroup><ItemGroup>
  166.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  167. </ItemGroup><ItemGroup>
  168.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  169. </ItemGroup><ItemGroup>
  170.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  171. </ItemGroup><ItemGroup>
  172.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  173. </ItemGroup><ItemGroup>
  174.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  175. </ItemGroup><ItemGroup>
  176.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  177. </ItemGroup>context.Succeed(requirement);<ItemGroup>
  178.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  179. </ItemGroup><ItemGroup>
  180.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  181. </ItemGroup><ItemGroup>
  182.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  183. </ItemGroup><ItemGroup>
  184.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  185. </ItemGroup><ItemGroup>
  186.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  187. </ItemGroup><ItemGroup>
  188.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  189. </ItemGroup>}<ItemGroup>
  190.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  191. </ItemGroup><ItemGroup>
  192.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  193. </ItemGroup><ItemGroup>
  194.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  195. </ItemGroup><ItemGroup>
  196.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  197. </ItemGroup><ItemGroup>
  198.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  199. </ItemGroup><ItemGroup>
  200.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  201. </ItemGroup>else<ItemGroup>
  202.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  203. </ItemGroup><ItemGroup>
  204.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  205. </ItemGroup><ItemGroup>
  206.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  207. </ItemGroup><ItemGroup>
  208.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  209. </ItemGroup><ItemGroup>
  210.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  211. </ItemGroup><ItemGroup>
  212.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  213. </ItemGroup>{<ItemGroup>
  214.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  215. </ItemGroup><ItemGroup>
  216.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  217. </ItemGroup><ItemGroup>
  218.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  219. </ItemGroup><ItemGroup>
  220.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  221. </ItemGroup><ItemGroup>
  222.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  223. </ItemGroup><ItemGroup>
  224.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  225. </ItemGroup><ItemGroup>
  226.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  227. </ItemGroup><ItemGroup>
  228.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  229. </ItemGroup>context.Fail();<ItemGroup>
  230.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  231. </ItemGroup><ItemGroup>
  232.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  233. </ItemGroup><ItemGroup>
  234.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  235. </ItemGroup><ItemGroup>
  236.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  237. </ItemGroup><ItemGroup>
  238.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  239. </ItemGroup><ItemGroup>
  240.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  241. </ItemGroup>}<ItemGroup>
  242.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  243. </ItemGroup><ItemGroup>
  244.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  245. </ItemGroup><ItemGroup>
  246.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  247. </ItemGroup><ItemGroup>
  248.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  249. </ItemGroup><ItemGroup>
  250.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  251. </ItemGroup><ItemGroup>
  252.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  253. </ItemGroup>return Task.CompletedTask;<ItemGroup>
  254.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  255. </ItemGroup><ItemGroup>
  256.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  257. </ItemGroup><ItemGroup>
  258.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  259. </ItemGroup><ItemGroup>
  260.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  261. </ItemGroup>}<ItemGroup>
  262.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  263. </ItemGroup><ItemGroup>
  264.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  265. </ItemGroup>}}
复制代码
客户端调用

Program.cs
  1. builder.Services.AddGrpcClient(options =>{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>options.Address = new Uri("https://localhost:7166");}).AddInterceptor().ConfigureChannel(async config =>{<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>//所有调用自动添加 Authorization<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup>CallCredentials credentials = CallCredentials.FromInterceptor(async (context, metadata) =><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>{<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>string token = await HttpClientHelper.HttpGetAsync("https://localhost:7117/api/Authenorization?userName=admin&passWord=666");<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup>metadata.Add("Authorization", $"Bearer {token}");<ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>});<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup>config.Credentials = ChannelCredentials.Create(new SslCredentials(), credentials);});
复制代码
HttpClientHelper.cs
  1. using Newtonsoft.Json;using System.Text;namespace gRpcWebAPI.Utility{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>public static class HttpClientHelper<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>{<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup><ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>public static async Task HttpGetAsync(string url, string contentType = "application/json", Dictionary headers = null)<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>{<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup><ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup>using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())<ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup><ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup>{<ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup><ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup><ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup>if (contentType != null)<ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup><ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup><ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup><ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup>client.DefaultRequestHeaders.Add("ContentType", contentType);<ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup><ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup>if (headers != null)<ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup><ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup><ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup>{<ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup><ItemGroup>
  122.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  123. </ItemGroup><ItemGroup>
  124.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  125. </ItemGroup><ItemGroup>
  126.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  127. </ItemGroup><ItemGroup>
  128.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  129. </ItemGroup><ItemGroup>
  130.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  131. </ItemGroup><ItemGroup>
  132.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  133. </ItemGroup><ItemGroup>
  134.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  135. </ItemGroup><ItemGroup>
  136.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  137. </ItemGroup>foreach (var header in headers)<ItemGroup>
  138.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  139. </ItemGroup><ItemGroup>
  140.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  141. </ItemGroup><ItemGroup>
  142.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  143. </ItemGroup><ItemGroup>
  144.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  145. </ItemGroup><ItemGroup>
  146.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  147. </ItemGroup><ItemGroup>
  148.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  149. </ItemGroup><ItemGroup>
  150.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  151. </ItemGroup><ItemGroup>
  152.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  153. </ItemGroup><ItemGroup>
  154.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  155. </ItemGroup><ItemGroup>
  156.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  157. </ItemGroup><ItemGroup>
  158.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  159. </ItemGroup><ItemGroup>
  160.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  161. </ItemGroup>client.DefaultRequestHeaders.Add(header.Key, header.Value);<ItemGroup>
  162.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  163. </ItemGroup><ItemGroup>
  164.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  165. </ItemGroup><ItemGroup>
  166.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  167. </ItemGroup><ItemGroup>
  168.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  169. </ItemGroup><ItemGroup>
  170.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  171. </ItemGroup><ItemGroup>
  172.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  173. </ItemGroup><ItemGroup>
  174.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  175. </ItemGroup><ItemGroup>
  176.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  177. </ItemGroup>}<ItemGroup>
  178.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  179. </ItemGroup><ItemGroup>
  180.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  181. </ItemGroup><ItemGroup>
  182.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  183. </ItemGroup><ItemGroup>
  184.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  185. </ItemGroup><ItemGroup>
  186.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  187. </ItemGroup><ItemGroup>
  188.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  189. </ItemGroup><ItemGroup>
  190.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  191. </ItemGroup><ItemGroup>
  192.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  193. </ItemGroup>HttpResponseMessage response = await client.GetAsync(url);<ItemGroup>
  194.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  195. </ItemGroup><ItemGroup>
  196.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  197. </ItemGroup><ItemGroup>
  198.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  199. </ItemGroup><ItemGroup>
  200.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  201. </ItemGroup><ItemGroup>
  202.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  203. </ItemGroup><ItemGroup>
  204.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  205. </ItemGroup><ItemGroup>
  206.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  207. </ItemGroup><ItemGroup>
  208.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  209. </ItemGroup>return await response.Content.ReadAsStringAsync();<ItemGroup>
  210.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  211. </ItemGroup><ItemGroup>
  212.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  213. </ItemGroup><ItemGroup>
  214.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  215. </ItemGroup><ItemGroup>
  216.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  217. </ItemGroup><ItemGroup>
  218.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  219. </ItemGroup><ItemGroup>
  220.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  221. </ItemGroup>}<ItemGroup>
  222.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  223. </ItemGroup><ItemGroup>
  224.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  225. </ItemGroup><ItemGroup>
  226.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  227. </ItemGroup><ItemGroup>
  228.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  229. </ItemGroup>}<ItemGroup>
  230.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  231. </ItemGroup><ItemGroup>
  232.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  233. </ItemGroup>}}
复制代码
gRpcController.cs
  1. using Custom.Service;using Grpc.Core;using gRpcWebAPI.Utility;using Microsoft.AspNetCore.Mvc;using Newtonsoft.Json;using System.Security.Cryptography.X509Certificates;using static Custom.Service.CustomGreeter;namespace gRpcWebAPI.Controllers{<ItemGroup>
  2.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3. </ItemGroup><ItemGroup>
  4.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5. </ItemGroup>[Route("api/[controller]/[action]")]<ItemGroup>
  6.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7. </ItemGroup><ItemGroup>
  8.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9. </ItemGroup>[ApiController]<ItemGroup>
  10.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11. </ItemGroup><ItemGroup>
  12.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13. </ItemGroup>public class gRpcController : ControllerBase<ItemGroup>
  14.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15. </ItemGroup><ItemGroup>
  16.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17. </ItemGroup>{<ItemGroup>
  18.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19. </ItemGroup><ItemGroup>
  20.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21. </ItemGroup><ItemGroup>
  22.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23. </ItemGroup><ItemGroup>
  24.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25. </ItemGroup>CustomGreeterClient _client;<ItemGroup>
  26.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27. </ItemGroup><ItemGroup>
  28.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29. </ItemGroup><ItemGroup>
  30.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31. </ItemGroup><ItemGroup>
  32.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33. </ItemGroup>public gRpcController(CustomGreeterClient client)<ItemGroup>
  34.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35. </ItemGroup><ItemGroup>
  36.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37. </ItemGroup><ItemGroup>
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39. </ItemGroup><ItemGroup>
  40.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41. </ItemGroup>{<ItemGroup>
  42.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43. </ItemGroup><ItemGroup>
  44.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45. </ItemGroup><ItemGroup>
  46.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47. </ItemGroup><ItemGroup>
  48.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49. </ItemGroup><ItemGroup>
  50.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  51. </ItemGroup><ItemGroup>
  52.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  53. </ItemGroup>_client = client;<ItemGroup>
  54.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  55. </ItemGroup><ItemGroup>
  56.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  57. </ItemGroup><ItemGroup>
  58.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  59. </ItemGroup><ItemGroup>
  60.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  61. </ItemGroup>}<ItemGroup>
  62.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  63. </ItemGroup><ItemGroup>
  64.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  65. </ItemGroup><ItemGroup>
  66.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  67. </ItemGroup><ItemGroup>
  68.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69. </ItemGroup>[HttpGet]<ItemGroup>
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71. </ItemGroup><ItemGroup>
  72.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  73. </ItemGroup><ItemGroup>
  74.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75. </ItemGroup><ItemGroup>
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77. </ItemGroup>public async Task AsyncPlus(int leftNumber, int rightNumber)<ItemGroup>
  78.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  79. </ItemGroup><ItemGroup>
  80.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81. </ItemGroup><ItemGroup>
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83. </ItemGroup><ItemGroup>
  84.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  85. </ItemGroup>{<ItemGroup>
  86.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  87. </ItemGroup><ItemGroup>
  88.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  89. </ItemGroup><ItemGroup>
  90.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  91. </ItemGroup><ItemGroup>
  92.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  93. </ItemGroup><ItemGroup>
  94.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  95. </ItemGroup><ItemGroup>
  96.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  97. </ItemGroup>try<ItemGroup>
  98.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  99. </ItemGroup><ItemGroup>
  100.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  101. </ItemGroup><ItemGroup>
  102.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  103. </ItemGroup><ItemGroup>
  104.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  105. </ItemGroup><ItemGroup>
  106.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  107. </ItemGroup><ItemGroup>
  108.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  109. </ItemGroup>{<ItemGroup>
  110.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  111. </ItemGroup><ItemGroup>
  112.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  113. </ItemGroup><ItemGroup>
  114.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  115. </ItemGroup><ItemGroup>
  116.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  117. </ItemGroup><ItemGroup>
  118.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  119. </ItemGroup><ItemGroup>
  120.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  121. </ItemGroup><ItemGroup>
  122.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  123. </ItemGroup><ItemGroup>
  124.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  125. </ItemGroup>//string token = await HttpClientHelper.HttpGetAsync("https://localhost:7117/api/Authenorization?userName=admin&passWord=666");<ItemGroup>
  126.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  127. </ItemGroup><ItemGroup>
  128.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  129. </ItemGroup><ItemGroup>
  130.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  131. </ItemGroup><ItemGroup>
  132.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  133. </ItemGroup><ItemGroup>
  134.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  135. </ItemGroup><ItemGroup>
  136.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  137. </ItemGroup><ItemGroup>
  138.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  139. </ItemGroup><ItemGroup>
  140.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  141. </ItemGroup>//Metadata jwtCode = new Metadata { { "Authorization", $"Bearer {token}" } };<ItemGroup>
  142.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  143. </ItemGroup><ItemGroup>
  144.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  145. </ItemGroup><ItemGroup>
  146.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  147. </ItemGroup><ItemGroup>
  148.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  149. </ItemGroup><ItemGroup>
  150.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  151. </ItemGroup><ItemGroup>
  152.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  153. </ItemGroup><ItemGroup>
  154.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  155. </ItemGroup><ItemGroup>
  156.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  157. </ItemGroup>NumberResult number = await _client.PlusAsync(new Custom.Service.Number() { LeftNumber = leftNumber, RightNumber = rightNumber });<ItemGroup>
  158.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  159. </ItemGroup><ItemGroup>
  160.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  161. </ItemGroup><ItemGroup>
  162.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  163. </ItemGroup><ItemGroup>
  164.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  165. </ItemGroup><ItemGroup>
  166.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  167. </ItemGroup><ItemGroup>
  168.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  169. </ItemGroup><ItemGroup>
  170.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  171. </ItemGroup><ItemGroup>
  172.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  173. </ItemGroup>return new JsonResult(number);<ItemGroup>
  174.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  175. </ItemGroup><ItemGroup>
  176.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  177. </ItemGroup><ItemGroup>
  178.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  179. </ItemGroup><ItemGroup>
  180.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  181. </ItemGroup><ItemGroup>
  182.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  183. </ItemGroup><ItemGroup>
  184.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  185. </ItemGroup>}<ItemGroup>
  186.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  187. </ItemGroup><ItemGroup>
  188.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  189. </ItemGroup><ItemGroup>
  190.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  191. </ItemGroup><ItemGroup>
  192.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  193. </ItemGroup><ItemGroup>
  194.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  195. </ItemGroup><ItemGroup>
  196.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  197. </ItemGroup>catch (Exception ex)<ItemGroup>
  198.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  199. </ItemGroup><ItemGroup>
  200.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  201. </ItemGroup><ItemGroup>
  202.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  203. </ItemGroup><ItemGroup>
  204.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  205. </ItemGroup><ItemGroup>
  206.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  207. </ItemGroup><ItemGroup>
  208.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  209. </ItemGroup>{<ItemGroup>
  210.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  211. </ItemGroup><ItemGroup>
  212.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  213. </ItemGroup><ItemGroup>
  214.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  215. </ItemGroup><ItemGroup>
  216.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  217. </ItemGroup><ItemGroup>
  218.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  219. </ItemGroup><ItemGroup>
  220.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  221. </ItemGroup><ItemGroup>
  222.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  223. </ItemGroup><ItemGroup>
  224.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  225. </ItemGroup>return new JsonResult(ex.Message);<ItemGroup>
  226.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  227. </ItemGroup><ItemGroup>
  228.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  229. </ItemGroup><ItemGroup>
  230.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  231. </ItemGroup><ItemGroup>
  232.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  233. </ItemGroup><ItemGroup>
  234.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  235. </ItemGroup><ItemGroup>
  236.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  237. </ItemGroup>}<ItemGroup>
  238.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  239. </ItemGroup><ItemGroup>
  240.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  241. </ItemGroup><ItemGroup>
  242.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  243. </ItemGroup><ItemGroup>
  244.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  245. </ItemGroup>}<ItemGroup>
  246.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  247. </ItemGroup><ItemGroup>
  248.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  249. </ItemGroup><ItemGroup>
  250.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  251. </ItemGroup><ItemGroup>
  252.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  253. </ItemGroup>[HttpGet]<ItemGroup>
  254.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  255. </ItemGroup><ItemGroup>
  256.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  257. </ItemGroup><ItemGroup>
  258.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  259. </ItemGroup><ItemGroup>
  260.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  261. </ItemGroup>public IActionResult Plus(int leftNumber, int rightNumber)<ItemGroup>
  262.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  263. </ItemGroup><ItemGroup>
  264.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  265. </ItemGroup><ItemGroup>
  266.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  267. </ItemGroup><ItemGroup>
  268.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  269. </ItemGroup>{<ItemGroup>
  270.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  271. </ItemGroup><ItemGroup>
  272.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  273. </ItemGroup><ItemGroup>
  274.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  275. </ItemGroup><ItemGroup>
  276.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  277. </ItemGroup><ItemGroup>
  278.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  279. </ItemGroup><ItemGroup>
  280.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  281. </ItemGroup>try<ItemGroup>
  282.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  283. </ItemGroup><ItemGroup>
  284.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  285. </ItemGroup><ItemGroup>
  286.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  287. </ItemGroup><ItemGroup>
  288.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  289. </ItemGroup><ItemGroup>
  290.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  291. </ItemGroup><ItemGroup>
  292.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  293. </ItemGroup>{<ItemGroup>
  294.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  295. </ItemGroup><ItemGroup>
  296.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  297. </ItemGroup><ItemGroup>
  298.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  299. </ItemGroup><ItemGroup>
  300.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  301. </ItemGroup><ItemGroup>
  302.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  303. </ItemGroup><ItemGroup>
  304.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  305. </ItemGroup><ItemGroup>
  306.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  307. </ItemGroup><ItemGroup>
  308.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  309. </ItemGroup>string token = HttpClientHelper.HttpGet("https://localhost:7117/api/Authenorization?userName=admin&passWord=666");<ItemGroup>
  310.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  311. </ItemGroup><ItemGroup>
  312.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  313. </ItemGroup><ItemGroup>
  314.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  315. </ItemGroup><ItemGroup>
  316.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  317. </ItemGroup><ItemGroup>
  318.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  319. </ItemGroup><ItemGroup>
  320.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  321. </ItemGroup><ItemGroup>
  322.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  323. </ItemGroup><ItemGroup>
  324.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  325. </ItemGroup>Metadata jwtCode = new Metadata { { "Authorization",$"Bearer {token}"} };<ItemGroup>
  326.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  327. </ItemGroup><ItemGroup>
  328.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  329. </ItemGroup><ItemGroup>
  330.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  331. </ItemGroup><ItemGroup>
  332.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  333. </ItemGroup><ItemGroup>
  334.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  335. </ItemGroup><ItemGroup>
  336.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  337. </ItemGroup><ItemGroup>
  338.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  339. </ItemGroup><ItemGroup>
  340.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  341. </ItemGroup>NumberResult number = _client.Plus(new Custom.Service.Number() { LeftNumber = leftNumber, RightNumber = rightNumber },headers: jwtCode);<ItemGroup>
  342.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  343. </ItemGroup><ItemGroup>
  344.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  345. </ItemGroup><ItemGroup>
  346.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  347. </ItemGroup><ItemGroup>
  348.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  349. </ItemGroup><ItemGroup>
  350.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  351. </ItemGroup><ItemGroup>
  352.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  353. </ItemGroup><ItemGroup>
  354.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  355. </ItemGroup><ItemGroup>
  356.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  357. </ItemGroup>return new JsonResult(number);<ItemGroup>
  358.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  359. </ItemGroup><ItemGroup>
  360.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  361. </ItemGroup><ItemGroup>
  362.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  363. </ItemGroup><ItemGroup>
  364.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  365. </ItemGroup><ItemGroup>
  366.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  367. </ItemGroup><ItemGroup>
  368.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  369. </ItemGroup>}<ItemGroup>
  370.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  371. </ItemGroup><ItemGroup>
  372.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  373. </ItemGroup><ItemGroup>
  374.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  375. </ItemGroup><ItemGroup>
  376.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  377. </ItemGroup><ItemGroup>
  378.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  379. </ItemGroup><ItemGroup>
  380.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  381. </ItemGroup>catch (Exception ex)<ItemGroup>
  382.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  383. </ItemGroup><ItemGroup>
  384.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  385. </ItemGroup><ItemGroup>
  386.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  387. </ItemGroup><ItemGroup>
  388.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  389. </ItemGroup><ItemGroup>
  390.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  391. </ItemGroup><ItemGroup>
  392.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  393. </ItemGroup>{<ItemGroup>
  394.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  395. </ItemGroup><ItemGroup>
  396.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  397. </ItemGroup><ItemGroup>
  398.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  399. </ItemGroup><ItemGroup>
  400.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  401. </ItemGroup><ItemGroup>
  402.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  403. </ItemGroup><ItemGroup>
  404.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  405. </ItemGroup><ItemGroup>
  406.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  407. </ItemGroup><ItemGroup>
  408.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  409. </ItemGroup>return new JsonResult(ex.Message);<ItemGroup>
  410.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  411. </ItemGroup><ItemGroup>
  412.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  413. </ItemGroup><ItemGroup>
  414.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  415. </ItemGroup><ItemGroup>
  416.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  417. </ItemGroup><ItemGroup>
  418.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  419. </ItemGroup><ItemGroup>
  420.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  421. </ItemGroup>}<ItemGroup>
  422.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  423. </ItemGroup><ItemGroup>
  424.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  425. </ItemGroup><ItemGroup>
  426.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  427. </ItemGroup><ItemGroup>
  428.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  429. </ItemGroup>}<ItemGroup>
  430.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  431. </ItemGroup><ItemGroup>
  432.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  433. </ItemGroup>}}
复制代码
Rpc 与 Restful 区别



  • RPC是以一种调用本地方法的思路来调用远程方法,通过各种RPC框架隐藏调用远程方法的细节,让用户以为调用的就是本地方法。RPC隐藏了底层网络通信的复杂度,让我们更专注于业务逻辑的开发。
  • REST通过HTTP实现,把用户的需求抽象成对资源的操作,用户必须通过HTTP协议的GET、HEAD、POST、PUT、DELETE、TRACE、OPTIONS七种基本操作去和服务器交互。
  • RPC通常是服务器和服务器之间的通信,比如和中间件的通信,MQ、分布式缓存、分布式数据库等等。
  • 而REST通常是面向客户端的(一般是浏览器),他们的使用场景也是不一样的。

来源:https://www.cnblogs.com/qfccc/archive/2023/08/09/17617999.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具