Exemple #1
0
        public ChannelReader <string> WeatherStream(CancellationToken cancellationToken)
        {
            var channel = Channel.CreateUnbounded <string>();

            _ = WriteItemsAsync(channel.Writer);

            return(channel.Reader);

            async Task WriteItemsAsync(ChannelWriter <string> writer)
            {
                using var replies = _client.GetWeatherStream(new WeatherRequest(), cancellationToken: cancellationToken);

                try
                {
                    await foreach (var forecast in replies.ResponseStream.ReadAllAsync())
                    {
                        var date = DateTimeOffset.FromUnixTimeSeconds(forecast.DateTimeStamp);
                        await writer.WriteAsync($"{date:s} | {forecast.Summary} | {forecast.TemperatureC} C", cancellationToken);
                    }
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
                {
                    replies.Dispose();
                    Console.WriteLine("Stream cancelled.");
                }
                catch (Exception ex)
                {
                    writer.TryComplete(ex);
                }

                writer.TryComplete();
            }
        }
Exemple #2
0
        private static async Task Main(string[] args)
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5005");
            var client  = new WeatherForecasts.WeatherForecastsClient(channel);

            var cts = new CancellationTokenSource();

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

            using var replies = client.GetWeatherStream(new WeatherRequest(), cancellationToken: cts.Token);

            try
            {
                await foreach (var weatherData in replies.ResponseStream.ReadAllAsync(cancellationToken: cts.Token))
                {
                    var date = DateTimeOffset.FromUnixTimeSeconds(weatherData.DateTimeStamp);

                    Console.WriteLine($"{date:s} | {weatherData.Summary} | {weatherData.TemperatureC} C");
                }

                while (await replies.ResponseStream.MoveNext(cts.Token))
                {
                }
            }
            catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
            {
                replies.Dispose();

                Console.WriteLine("Stream cancelled.");
            }

            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
        }
Exemple #3
0
        static async Task Main(string[] args)
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client  = new WeatherForecasts.WeatherForecastsClient(channel);

            var dataStream        = client.GetWeatherStream(new Empty());
            var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token;

            try
            {
                await foreach (var weatherData in dataStream.ResponseStream.ReadAllAsync(cancellationToken))
                {
                    Console.WriteLine(weatherData.DateTimeStamp + ": " + weatherData.DegreesCelsius + "C, humidity "
                                      + weatherData.Humidity + "%");
                }
            }
            catch (RpcException e) when(e.StatusCode == StatusCode.Cancelled)
            {
                Console.WriteLine("Stream cancelled by client.");
            }
        }