I have a couple of web-requests that I want to perform and that are independent. So I put all these tasks into a loop and use WhenAll
to just await
when all tasks finished.
var tasks = new List<Task>();
foreach (var component in doc.Descendants("component"))
{
tasks.Add(DoSomething());
}
await Task.WhenAll(tasks);
where DoSomething
is this:
async Task DoSomething() => await client.PerformRequest();
Now I have a problem as the service is quite unstable and many of these requests fail. So I just want to re-send them for a configurable amount, say five times. So when one request fails, just retry it. How can I do that?
I've tried this, however that doesn't return a Task
anymore and therefor there's no way to use Task.WhenAll
.
async void DoSomething()
{
for (int i = 0; i < 5; i++)
{
try
{
await client1.PerformRequest();
break;
}
catch
{
Console.WriteLine("retrying");
}
}
}