I have a basic .Net Winform desktop application. I'd like to make multiple http calls to an Api from the application, one http call takes about 50s to complete, let's call the method GetApiDataAsync()
. I want to call GetApiDataAsync()
500+ times. Synchronous would be too slow so my goal is to run as many as I can in parallel. I've done this by creating a list of tasks and awaiting it ( var results = await Task.WhenAll(taskList)
) but I've noticed that when the size of the list takes greater than about 1.5 min I get an exception that says the "Task was canceled". 1.5 min correlates with about 4 tasks, if the Task.WhenAll
awaits more than that it seems like that's when the exception is thrown.
This is what my code looks like:
The Api Call
private HttpClient _client = new HttpClient();
public async Task<DataObject> GetApiDataAsync(int id)
{
return await _client.Get($"/entity/{id}");
}
The method that bundles the Api Calls
public async Task<IList<DataObject>> GetDataObjects(IList<int> ids)
{
var requests = ids.Select(x => GetApiDataAsync(x));
var responses = await Task.WhenAll(requests).ConfigureAwait(false);
return responses.ToList();
}
The UI action that initiates
public async void ButtonClick()
{
//First thing I tried
var data = await GetDataObjects(ids)
//Second thing I tried
var data = await Task.Run(() => GetDataObjects(ids));
dataGridView = data;
}
I tried both methods above but both result in task cancelation if more than 1.5 mins passes (about 4 tasks).
Questions:
Which task is being canceled?
What is canceling the task?
Why is the task being canceled?
How can I successfuly run multiple http requests in parallel?