Esempio n. 1
0
        private static async Task WeatherForecastsRequest(GrpcChannel channel, string token)
        {
            var headers = new Metadata();

            headers.Add("Authorization", $"Bearer {token}");

            var client = new WeatherForecastsClient(channel);

            var cts           = new CancellationTokenSource(TimeSpan.FromSeconds(2));
            var streamingCall = client.GetWeatherStream(new Empty(), cancellationToken: cts.Token, headers: headers);

            try
            {
                await foreach (var weatherData in streamingCall.ResponseStream.ReadAllAsync(cancellationToken: cts.Token))
                {
                    Console.WriteLine($"{weatherData.DateTimeStamp.ToDateTime():s} | {weatherData.Summary} | {weatherData.TemperatureC} C");
                }
            }
            catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
            {
                Console.WriteLine("Stream cancelled.");
            }
            catch (IOException) // https://github.com/dotnet/runtime/issues/1586
            {
                Console.WriteLine("Client and server disagree on active stream count.");
            }
        }
Esempio n. 2
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 Empty(), cancellationToken: cancellationToken);

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

                writer.TryComplete();
            }
        }
Esempio n. 3
0
        static async Task Main(string[] args)
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5001");

            //var clientGreet = new Greeter.GreeterClient(channel);
            //var requestGreet = new HelloRequest() { Name = "Pedro" };
            //var reply = await clientGreet.SayHelloAsync(requestGreet);
            //Console.WriteLine("greeter: " + reply.Message);

            //var clientCustomer = new Customers.CustomersClient(channel);

            //var request = new CustomerModelRequest { UserId = 1 };
            //var result = await clientCustomer.GetCustomerInforAsync(request);
            //Console.WriteLine($"Customer: {result.Name} {result.Age} {result.IsActive}");

            //using (var call = clientCustomer.GetCustomers(new CustomersRequest()))
            //{
            //    Console.WriteLine("lista customers");
            //    while (await call.ResponseStream.MoveNext())
            //    {
            //        var customer = call.ResponseStream.Current;
            //        Console.WriteLine($"{customer.Name} {customer.Age} {customer.IsActive}");
            //    }
            //}


            var client = new WeatherForecastsClient(channel);

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

            using var streamingCall = client.GetWeatherStream(new Empty(), cancellationToken: cts.Token);

            try
            {
                await foreach (var weatherData in streamingCall.ResponseStream.ReadAllAsync(cancellationToken: cts.Token))
                {
                    Console.WriteLine($"{weatherData.DateTimeStamp.ToDateTime():s} | {weatherData.Summary} | {weatherData.TemperatureC} C");
                }
            }
            catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
            {
                Console.WriteLine("Stream cancelled.");
            }
            Console.ReadLine();
        }
Esempio n. 4
0
        private static async Task Main()
        {
            using var channel = GrpcChannel.ForAddress("https://localhost:5005");
            var client = new WeatherForecastsClient(channel);

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

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

            try
            {
                await foreach (var weatherData in replies.ResponseStream.ReadAllAsync(cancellationToken: cts.Token))
                {
                    Console.WriteLine($"{weatherData.DateTimeStamp.ToDateTime():s} | {weatherData.Summary} | {weatherData.TemperatureC} C");
                }
            }
            catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
            {
                Console.WriteLine("Stream cancelled.");
            }

            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
        }