I am using GetAsync method to read a web api get method through the following code
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:10000/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/account/balance/" + accountNumber.ToString()).Result;
if (response.IsSuccessStatusCode)
{
string responseJson = response.Content.ReadAsStringAsync().Result;
JavaScriptSerializer jss = new JavaScriptSerializer();
var result = jss.Deserialize<AccountResponse>(responseJson);
}
else
{
Console.WriteLine("Error! Http error " + response.StatusCode.ToString());
Console.ReadLine();
}
I am expecting when there is a problem with the service like authentication, page not found, or else, the relevant HTTP code should be returned and found in the HttpResponseMessage instance, but what happens is that when one of these HTTP error exist, the HttpResponseMessage throws an AggregateException when trying to GetAsync