I have some trouble choosing the best way to go with asynchronous requests to a web API. I need to get data from sequential webrequests, with the following requirements :
- requests must obviously not block GUI (async or in another thread)
- each request returns a list of records, that I want to merge immediately with my app's data
- requests must not run at the same time (preserve "logical" order)
- if a request fails, remaining requests must be cancelled
The web requests are not CPU intensive, just slow. Moreover, since I want to merge results after every request, using different threads would cause some synchronization overhead.
That's why I thought better to use async/await to avoid creating new threads. First, is this assumption right ?
My (incomplete) solution :
I came up with an elegant solution using yield
, but it doesn't implement the expected async
behavior.
Here is the simplified API's connector code :
public IEnumerable<Record[]> GetRecordsInRange(DateTime startDate, DateTime endDate)
{
while (startDate <= endDate)
{
List<Record> results = new List<Record>();
try
{
results.AddRange(ExecuteQueryAPI(startDate, QueryTypes.A));
results.AddRange(ExecuteQueryAPI(startDate, QueryTypes.B));
results.AddRange(ExecuteQueryAPI(startDate, QueryTypes.C));
}
catch (Exception ex)
{
// Cancel remaining requests
yield break;
}
// Return partial results.
yield return results.ToArray();
// Jump to the next day.
startDate = startDate.AddDays(1);
}
yield break;
}
Usage :
foreach (var records in APIConnector.GetRecordsInRange(DateTime.Now.AddDays(-10), DateTime.Now))
{
Records.AddRange(records);
}
Is it possible to implement an aync/await behavior to the previous code, or should I use a different approach ?