Exemple #1
0
        //ClientStreamingRPC gRPC--客户端流式
        //传入多个请求对象,返回一个结果对象,分批传入分批处理,统一返回
        private static async Task SelfIncreaseClientRPCMethod()
        {
            using (var channel = GrpcChannel.ForAddress("https://localhost:5001"))
            {
                var client = new Test.TestClient(channel);
                var reply  = client.SelfIncreaseClient();
                for (int i = 0; i < 10; i++)
                {
                    int id = new Random(Guid.NewGuid().GetHashCode()).Next(0, 20);
                    Console.WriteLine($"发送数据{id}");
                    await reply.RequestStream.WriteAsync(new BatchTheCatReq()
                    {
                        Id = id
                    });

                    await Task.Delay(100);
                }

                await reply.RequestStream.CompleteAsync();

                Console.WriteLine("接收处理结果");
                foreach (var item in reply.ResponseAsync.Result.Number)
                {
                    Console.WriteLine($"This is {item} Result");
                }
            }
        }