I am trying to find account details from DB (GetAccountDetailAsync) for an array of accounts and would like to run in parallel to make it faster.
[HttpPost]
public async Task<IHttpActionResult> GetAccountsAsync(IEnumerable<int> accountIds)
{
var resultAccounts = new List<AccountDetail>();
var task = Task.Run(() =>
{
Parallel.ForEach(accountIds, new ParallelOptions
{
MaxDegreeOfParallelism = 5
}, async accountId =>
{
var response = await GetAccountDetailAsync(accountId).ConfigureAwait(false);
resultAccounts.AddRange(response);
});
});
task.Wait();
return Ok(resultAccounts);
}
But instead of getting the result I am getting though I've got task.Wait. Not sure why task.Wait is not being blocked.
"An asynchronous module or handler completed while an asynchronous operation was still pending."