Esempio n. 1
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();
        }
Esempio n. 2
0
 public Worker(ILogger <Worker> logger, HttpClient httpClient,
               WeatherForecasts.WeatherForecastsClient weatherForecastsClientNet3,
               WeatherForecasts.WeatherForecastsClient weatherForecastsClientNet5,
               Stopwatch stopwatch,
               Func <string, Stopwatch, BenchMarks> benchMarks)
 {
     _logger     = logger;
     _httpClient = httpClient;
     _weatherForecastsClientNet3 = weatherForecastsClientNet3;
     _weatherForecastsClientNet5 = weatherForecastsClientNet5;
     _stopwatch  = stopwatch;
     _benchMarks = benchMarks;
 }
Esempio n. 3
0
        private static async Task Main(string[] args)
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5005");
            var client  = new WeatherForecasts.WeatherForecastsClient(channel);

            using var townForecast = client.ClientStreamWeather();

            var responseProcessing = Task.Run(async() =>
            {
                try
                {
                    await foreach (var forecast in townForecast.ResponseStream.ReadAllAsync())
                    {
                        var date = DateTimeOffset.FromUnixTimeSeconds(forecast.WeatherData.DateTimeStamp);

                        Console.WriteLine($"{forecast.TownName} = {date:s} | {forecast.WeatherData.Summary} | {forecast.WeatherData.TemperatureC} C");
                    }
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
                {
                    Console.WriteLine("Stream cancelled.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error reading response: " + ex);
                }
            });

            foreach (var town in Towns)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine($"Requesting forecast for {town}...");
                Console.ResetColor();

                await townForecast.RequestStream.WriteAsync(new TownWeatherRequest { TownName = town });

                await Task.Delay(2500); // simulate delay getting next item
            }

            Console.WriteLine("Completing request stream");
            await townForecast.RequestStream.CompleteAsync();

            Console.WriteLine("Request stream completed");

            await responseProcessing;

            Console.WriteLine("Read all responses");
            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
        }
Esempio n. 4
0
        static async Task Main(string[] args)
        {
            using var channel = GrpcChannel.ForAddress("https://localhost:4001");
            var clinet  = new WeatherForecasts.WeatherForecastsClient(channel);
            var results = await clinet.GetWeatherForecastsAsync(new GetWeatherForecastsRequest { ReturnCount = 100 });

            var table = new ConsoleTable("Date", "Temperature in C", "Summary");

            foreach (var bench in results.Forecasts)
            {
                table.AddRow(bench.Date, bench.TemperatureC, bench.Summary);
            }
            table.Write(Format.Default);
            Console.ReadLine();
        }
Esempio n. 5
0
        private static async Task Main(string[] args)
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5005");
            var client  = new WeatherForecasts.WeatherForecastsClient(channel);

            var response = await client.GetWeatherAsync(new WeatherRequest());

            foreach (var forecast in response.WeatherData)
            {
                var date = DateTimeOffset.FromUnixTimeSeconds(forecast.DateTimeStamp);

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

            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
        }
Esempio n. 6
0
        static async Task Main(string[] args)
        {
            //var channel = GrpcChannel.ForAddress("https://localhost:5001/");
            var channel = GrpcChannel.ForAddress("https://localhost:44304/", new GrpcChannelOptions()
            {
                HttpHandler = new GrpcWebHandler(new HttpClientHandler()),
            });

            var c = new WeatherForecasts.WeatherForecastsClient(channel);

            var e = await c.GetWeatherAsync(new WeatherForecast());

            foreach (var it in e.Forecasts)
            {
                Console.WriteLine(it);
            }
        }
Esempio n. 7
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.");
            }
        }
Esempio n. 8
0
 public WeatherStreamHub(WeatherForecasts.WeatherForecastsClient client)
 {
     _client = client;
 }