using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
public static async Task<int> Main()
using(var scope = host.Services.CreateScope())
var services = scope.ServiceProvider;
var weatherService = services.GetRequiredService<WeatherService>();
var report = await weatherService.GetWeatherReportAsync(InputList.Cities[0]);
Console.WriteLine(report);
Console.WriteLine($"Error Occured: {excep.Message}");
private static IHost GetHost()
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
services.AddHttpClient();
services.AddTransient<WeatherService>();
public class WeatherService
private readonly IHttpClientFactory _httpFactory;
public WeatherService(IHttpClientFactory httpFactory){
_httpFactory = httpFactory;
public async Task<string> GetWeatherReportAsync(Location location){
var client = _httpFactory.CreateClient();
var request = CreateOpenWeatherMapRequest(location);
var response = await client.SendAsync(request);
if(response.IsSuccessStatusCode){
return await response.Content.ReadAsStringAsync();
return $"Failure in fetching API response: {response.StatusCode}";
private HttpRequestMessage CreateOpenWeatherMapRequest(Location location){
var paramString = $"?q={location.City},{location.State}&appid={Config.CONFIDENTIAL_API_KEY}";
return new HttpRequestMessage(HttpMethod.Get, Config.URL_TO_HIT + paramString);
public const string URL_TO_HIT = "http://api.openweathermap.org/data/2.5/forecast";
public const string CONFIDENTIAL_API_KEY = "d2bb2b0318a1c3f0a7d04e7a3f2b3aba";
public string City {get; set;}
public string State{get; set;}
public static readonly List<Location> Cities = new List<Location>
new Location(){City="Marlboro", State="MA"},
new Location(){City="San Diego", State="CA"},
new Location(){City="Cheyenne", State="WY"},
new Location(){City="Anchorage", State="AK"},
new Location(){City="Austin", State="TX"},
new Location(){City="Orlando", State="FL"},
new Location(){City="Seattle", State="WA"},
new Location(){City="Cleveland", State="OH"},
new Location(){City="Portland", State="ME"},
new Location(){City="Honolulu", State="HI"},