Пример #1
0
        private static async Task ServerStreamingCallExample()
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client  = new GreeterClient(channel);
            var cts     = new CancellationTokenSource();

            cts.CancelAfter(TimeSpan.FromSeconds(80));

            using var call = client.SayHelloStream1(new HelloRequest { Name = "零度" }, cancellationToken: cts.Token);

            try
            {
                await foreach (var message in call.ResponseStream.ReadAllAsync())
                {
                    Console.WriteLine("Greeting: " + message.Message);
                }
            }
            catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
            {
                Console.WriteLine("Stream cancelled.");
            }
        }