public Task <HealthCheckResult> CheckHealthAsync(
            HealthCheckContext context,
            CancellationToken cancellationToken = default)
        {
            try
            {
                using (var channel = GrpcChannel.ForAddress(_host))
                {
                    var client = new Weather.WeatherClient(channel);
                    var result = client.WeatherData(new WeatherRequest());

                    var healthCheckResultHealthy = result.WeatherData.Any();

                    if (healthCheckResultHealthy)
                    {
                        return(Task.FromResult(
                                   HealthCheckResult.Healthy("A healthy result.")));
                    }
                }

                return(Task.FromResult(
                           HealthCheckResult.Unhealthy("An unhealthy result.")));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(
                           HealthCheckResult.Unhealthy(ex.Message)));
            }
        }
示例#2
0
        public async Task <WeatherReply> GetForecastsByGrpc()
        {
            _logger.Information("Executing vanilla Grpc call {GrpcUrl}", $"http://localhost:{WeatherServiceGrpcPort}");
            var channel  = GrpcChannel.ForAddress($"http://localhost:{WeatherServiceGrpcPort}");
            var client   = new Weather.WeatherClient(channel);
            var response = await client.GetForecastAsync(new Empty());

            return(response);
        }
示例#3
0
文件: Program.cs 项目: rdodgen/Ezri
 static void Main(string[] args)
 {
     var c = new Weather.WeatherClient();
     Console.WriteLine("Fetching weather...");
     var w = c.GetCurrentWeatherAsync().Result;
     Console.WriteLine("Current: {0} F ({1})", w.CurrentFahrenheitTemperature, w.CurrentConditions);
     Console.WriteLine("Today's forecast: {2} / High {0} F / Low {1} F / {3} percent change of precipication",
         w.HighFahrenheitTemperature, w.LowFahrenheitTemperature, w.ForecastConditions, w.ChanceOfPrecipitation);
     Console.ReadKey();
 }
        public async Task <IEnumerable <WeatherDataItem> > GetSecond()
        {
            return(await GrpcCallerService.CallService(_urls.WeatherSecond, async httpClient =>
            {
                var channel = GrpcChannel.ForAddress(_urls.WeatherSecond);
                var client = new Weather.WeatherClient(channel);
                _logger.LogDebug("grpc client created, request");
                var response = await client.GetAsync(new ApiSecond.Proto.WeatherRequest());
                _logger.LogDebug("grpc response {@response}", response);

                return MapToWeatherData(response);
            }));
        }
示例#5
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("create channel");
            using (var channel = GrpcChannel.ForAddress("https://localhost:5001"))
            {
                _logger.LogInformation("channel created");

                _logger.LogInformation("create client");
                var client = new Weather.WeatherClient(channel);
                _logger.LogInformation("client created");

                var d = new DateTime(2019, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                var i = 0;
                while (!stoppingToken.IsCancellationRequested)
                {
                    try
                    {
                        _logger.LogInformation("load weather data");
                        var request = new WeatherRequest
                        {
                            Date = Timestamp.FromDateTime(d)
                        };
                        var weather = await client.GetWeatherAsync(
                            request, null, null, stoppingToken);

                        _logger.LogInformation(
                            $"Temp: {weather.AvgTemperature}; " +
                            $"Precipitaion: {weather.Precipitaion}");

                        await _weatherService.Create(new WeatherData
                        {
                            Id             = i,
                            WeatherStation = "US1WAKG0045",
                            AvgTemperature = weather.AvgTemperature,
                            AvgWindSpeed   = weather.AvgWindSpeed,
                            MaxTemperature = weather.MaxTemperature,
                            MinTemperature = weather.MinTemperature,
                            Precipitaion   = weather.Precipitaion,
                            Date           = weather.Date.ToDateTime()
                        });
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, ex.Message);
                    }
                    d = d.AddDays(1);
                    i++;
                    await Task.Delay(1000, stoppingToken);
                }
            }
        }
示例#6
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            string name;

            do
            {
                using var channel = GrpcChannel.ForAddress("https://localhost:5001");
                var client = new Weather.WeatherClient(channel);
                Console.Write("Введите город или stop для выхода: ");
                name = Console.ReadLine();
                var reply = await client.GetWeatherAsync(new WeatherRequest { Name = name });

                Console.WriteLine("Ответ сервера: " + reply.Message);
            } while (name != "stop");
        }
示例#7
0
        public void OnGet()
        {
            var weatherChannel = GrpcChannel.ForAddress("https://localhost:5002");
            var weather        = new Weather.WeatherClient(weatherChannel);
            var temp           = weather.Temperature(new LocationMessage {
                Name = "Bob"
            });

            TemperatureCelcius = temp.Celsius;

            var todoChannel = GrpcChannel.ForAddress("https://localhost:5003");
            var todoClient  = new Todo.TodoClient(todoChannel);
            var todos       = todoClient.GetTodos(new GetTodosMessage());

            Todos = todos.Todos;
        }
示例#8
0
        public static async Task Main()
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client  = new Weather.WeatherClient(channel);

            var weather = new WeatherForecast {
                Summary = "Hot"
            };
            var reply = await client.SaveForecastAsync(weather);

            Console.WriteLine(reply.Success);

            var result = await client.GetForecastAsync(new Empty());

            Console.WriteLine(result.Summary);

            Console.ReadKey();
        }
示例#9
0
        static async Task Main(string[] args)
        {
            using var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client = new Greeter.GreeterClient(channel);
            var reply  = await client.SayHelloAsync(
                new HelloRequest { Name = "GreeterClient", Languages = "Java, C#" });

            Console.WriteLine("Greeting: " + reply.Message);

            var clientWaeather  = new Weather.WeatherClient(channel);
            var weatherResponse = await clientWaeather.GetWeatherAsync(new GetWeatherRequest { Name = "london" });


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

            Console.WriteLine("Hello World!");
        }
示例#10
0
        public async Task <WeatherResponse> GetWeather()
        {
            try
            {
                var httpClient = new HttpClient();
                var channel    = GrpcChannel.ForAddress(App.GRPCBackendUrl, new GrpcChannelOptions {
                    HttpClient = httpClient
                });
                var weatherClient = new Weather.WeatherClient(channel);
                var result        = await weatherClient.GetWeatherAsync(new Empty());

                return(result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        private async Task <List <WeatherDto> > GetWeatherData(ITracer tracer)
        {
            var clientUrl = "https://localhost:5002";
            var channel   = GrpcChannel.ForAddress(clientUrl);
            var client    = new Weather.WeatherClient(channel);

            var headers = new Metadata();

            var outgoingSpan = tracer.StartSpan($"Start to call {clientUrl} to get weather data", SpanKind.Client);

            if (outgoingSpan.Context.IsValid)
            {
                tracer.TextFormat.Inject(outgoingSpan.Context, headers, (headers, name, value) => headers.Add(name, value));
            }

            var result = await client.GetWeathersAsync(new GetWeathersRequest(), headers);

            outgoingSpan.End();

            return(result.Items.ToList());
        }