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."); } }
private async static Task InterceptorMethod(GrpcChannel channel) { var invoker = channel.Intercept(new ClientLoggerInterceptor()); var client = new WeatherForecastsClient(invoker); var result = await client.GetWeatherForecastsAsync(new Weather.GetWeatherForecastsRequest { ReturnCount = 100 }); }
private static async Task Main(string[] args) { using var channel = GrpcChannel.ForAddress("https://localhost:5005"); var client = new WeatherForecastsClient(channel); using var townForecast = client.GetTownWeatherStream(); var responseProcessing = Task.Run(async() => { try { await foreach (var forecast in townForecast.ResponseStream.ReadAllAsync()) { var date = forecast.WeatherData.DateTimeStamp.ToDateTime(); 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 new [] { "London", "Brighton", "Eastbourne", "Oxford", "Cambridge" }) { 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(); }
private async static Task ExceptionHandling(GrpcChannel channel) { var client = new WeatherForecastsClient(channel); try { await client.GetWeatherForecastsAsync(new Weather.GetWeatherForecastsRequest { ReturnCount = 1000000 }); } catch (RpcException ex) { Console.WriteLine(ex.Status.Detail); } }
private static async Task Main() { using var channel = GrpcChannel.ForAddress("https://localhost:5005"); var client = new WeatherForecastsClient(channel); var reply = await client.GetWeatherAsync(new Empty()); foreach (var forecast in reply.WeatherData) { Console.WriteLine($"{forecast.DateTimeStamp.ToDateTime():s} | {forecast.Summary} | {forecast.TemperatureC} C"); } Console.WriteLine("Press a key to exit"); Console.ReadKey(); }
private static async Task WithGrpcAsync() { // This switch must be set before creating the GrpcChannel/HttpClient. AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); using var channel = GrpcChannel.ForAddress("http://localhost:5001"); var client = new WeatherForecastsClient(channel); var reply = await client.GetWeatherAsync(new Empty()); Console.WriteLine("From GRPC"); foreach (var forecast in reply.WeatherData) { Console.WriteLine($"{forecast.DateTimeStamp.ToDateTime():s} | {forecast.Summary} | {forecast.TemperatureC} C"); } }
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(); }
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(); }
public WeatherStreamHub(WeatherForecastsClient client) { _client = client; }