I am writing an AddIn for ArcGIS 10.x using C#. I want to asynchronously download a file within ArcMap using HttpClient. My test code works in a hello world app but just hangs when I debug it in ArcMap.
Is it possible to write multi-threaded code using async within ArcMap AddIns?
private static async Task<raveInfo> GetLatestVersion(string url, HttpClient client)
{
client.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.ConnectionClose = true;
using (var httpResponse = await client.GetAsync(url))
{
httpResponse.EnsureSuccessStatusCode(); // throws if not 200-299
if (httpResponse.Content is object && httpResponse.Content.Headers.ContentType.MediaType == "application/json")
{
var contentStream = await httpResponse.Content.ReadAsStreamAsync();
using (var streamReader = new StreamReader(contentStream))
{
using (var jsonReader = new JsonTextReader(streamReader))
{
JsonSerializer serializer = new JsonSerializer();
try
{
return serializer.Deserialize<raveInfo>(jsonReader);
}
catch (JsonReaderException)
{
throw new Exception("Invalid JSON.");
}
}
}
}
else
{
throw new Exception("HTTP Response was invalid and cannot be deserialised.");
}
}
return null;
}