I need to make multiple webrequests where URIs are in a DataTable. Earlier I had the below code. But I realized this makes synchronous calls as await would wait till GET/POST call is complete and response is processed then it proceeds to next iteration.
foreach (DataRow dr in dt.Rows)
{
activeTasks.Add(SendRequestAsync(dr));
Task.WhenAll(activeTasks).Wait();
}
private async Task<string> SendRequestAsync(DataRow dr)
{
using (var client = new HttpClient())
{
string reqMethod = (dr["RequestMethod"] != null && dr["RequestMethod"].ToString() != "") ? dr["RequestMethod"].ToString() : "GET";
client.BaseAddress = new Uri(dr["URL"].ToString());
client.DefaultRequestHeaders.Accept.Clear();
string reqContentType = (dr["RequestContentType"] != null && dr["RequestContentType"].ToString() != "") ? dr["RequestContentType"].ToString() : "text/xml";
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(reqContentType));
HttpResponseMessage response = null;
try
{
if (reqMethod == "GET")
response = await client.GetAsync(client.BaseAddress.AbsoluteUri);
else
response = await client.PostAsync(client.BaseAddress.AbsoluteUri, null);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
return responseText;
}
catch (Exception e)
{
return "-1";
}
}
}
Then I came across Parallel feature and used Parallel.ForEach instead. Like this:
Parallel.ForEach(rows, dr =>
{
activeTasks.Add(SendRequestAsync(dr));
Task.WhenAll(activeTasks).Wait();
});
This works fine, parallelism is achieved, requests are asynchronous and it completes within fraction of a time as compared to earlier solution. But the problem is it is not reliable - at times I get errors like
- System.IndexOutOfRangeException: Index was outside the bounds of the array
- System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
Is there anyway we can achieve http async calls within a foreach?
Parallel.ForEach
for this. Really you just want to move yourTask.WhenAll
outside of the loop that you are adding tasks to your task list in. – Jonathon Chase