I am having issues which seem to be related calling a specific API asynchronously using HttpClient - the strange thing is that it doesn't happen all the time and can be solved by refreshing the page (sometimes once, sometimes multiple times).
I thought this could be a local issue but hosting on Azure produces the same results.
Raw exception details:
System.Net.Sockets.SocketException (11001): No such host is known at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
I have checked:
- There are no limits imposed on the API
- Passing the request url in a browser returns the expected JSON result
- Refreshing the page sometimes resolves the issue
This is the method that seems to be causing the issue:
public async Task<List<MoonPhase.Phasedata>> GetPhaseDataAsync(double lat, double lng, int year)
{
string requestUrl = "https://api.usno.navy.mil/moon/phase?year=" + year + "&coords=" + locationService.FormatCoordinates(lat, lng) + "&tz=0";
using (var client = new HttpClient())
{
var content = await client.GetStringAsync(requestUrl);
var moonPhaseObject = JsonConvert.DeserializeObject<MoonPhase.RootObject>(content, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
return moonPhaseObject.PhaseData;
}
}

