static async Task Main(string[] args)
        {
            try
            {
                Channel channel = new Channel(target, ChannelCredentials.Insecure);

                await channel.ConnectAsync().ContinueWith((task) =>
                {
                    if (task.Status == TaskStatus.RanToCompletion)
                    {
                        Console.WriteLine("The client connected successfully");
                    }
                });

                var client = new ComputeAverageService.ComputeAverageServiceClient(channel);

                var stream = client.ComputeAverage();

                string key;

                do
                {
                    Console.WriteLine(Environment.NewLine + "Digit a number or type 'ESC' to exit");
                    key = Console.ReadLine();

                    var isNumeric = int.TryParse(key, out int number);

                    if (key.ToLower() != esc && isNumeric)
                    {
                        await stream.RequestStream.WriteAsync(new ComputeAverageRequest()
                        {
                            Number = number
                        });
                    }
                } while (key.ToLower() != esc);

                await stream.RequestStream.CompleteAsync();

                var responseClientStream = await stream.ResponseAsync;

                Console.WriteLine(responseClientStream.ToString());

                channel.ShutdownAsync().Wait();
                Console.ReadLine();
            }
            catch (RpcException e)
            {
                Console.WriteLine($"StatusCode: {e.Status.StatusCode} | Detail: {e.Status.Detail}");
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong.");
            }
        }
Пример #2
0
        //Client Streaming Average
        public static async Task CalculateAverage(Channel channel)
        {
            var client = new ComputeAverageService.ComputeAverageServiceClient(channel);
            var stream = client.ComputeAverage();

            foreach (int i in Enumerable.Range(1, 4))
            {
                var request = new ComputeAverageRequest()
                {
                    Number = i
                };
                await stream.RequestStream.WriteAsync(request);
            }
            await stream.RequestStream.CompleteAsync();

            var response = await stream.ResponseAsync;

            Console.WriteLine(response.Result);
        }