Exemple #1
0
        //ServerStreamingRPC gRPC--服务端流式
        //传入一个请求对象,返回多个结果对象,服务端处理完成一个返回一个
        private static async Task ServerStreamingRPCMethod()
        {
            using (var channel = GrpcChannel.ForAddress("https://localhost:5001"))
            {
                var           client        = new Test.TestClient(channel);
                IntArrayModel intArrayModel = new IntArrayModel();
                for (int i = 1; i < 10; i++)
                {
                    intArrayModel.Number.Add(i);
                }

                //var cancellationToken = new CancellationTokenSource();
                //cancellationToken.CancelAfter(TimeSpan.FromSeconds(2.5));
                //var reply = client.SelfIncreaseServer(intArrayModel, cancellationToken: cancellationToken.Token);

                var reply = client.SelfIncreaseServer(intArrayModel);
                await Task.Run(async() =>
                {
                    await foreach (var resp in reply?.ResponseStream.ReadAllAsync())
                    {
                        Console.WriteLine(resp.Message);
                    }
                });
            }
        }