I'm trying to process each individual request as it finishes, which is what would happen in the ContinueWith
after the GetStringAsync
and then when they've all completed have an additional bit of processing.
However, it seems that the ContinueWith
on the WhenAll
fires right away. It appears as if the GetStringAsync
tasks are faulting, but I can't figure out why.
When I use WaitAll
instead of WhenAll
and just add my processing after the WaitAll
then my requests work just fine. But when I change it to WhenAll
it fails.
Here is an example of my code:
using (var client = new HttpClient())
{
Task.WhenAll(services.Select(x =>
{
return client.GetStringAsync(x.Url).ContinueWith(response =>
{
Console.WriteLine(response.Result);
}, TaskContinuationOptions.AttachedToParent);
}).ToArray())
.ContinueWith(response =>
{
Console.WriteLine("All tasks completed");
});
}