2

I've used the below code from this post - What is the best way to cal API calls in parallel in .net Core, C#?

It works fine, but when I'm processing a large list, some of the calls fail.

My question is, how can I implement Retry logic into this?

 foreach (var post in list)
        {
            async Task<string> func()
            {
                var response = await client.GetAsync("posts/" + post);
                return await response.Content.ReadAsStringAsync();
            }

            tasks.Add(func());
        }

        await Task.WhenAll(tasks);

        var postResponses = new List<string>();

        foreach (var t in tasks) {
            var postResponse = await t; //t.Result would be okay too.
            postResponses.Add(postResponse);
            Console.WriteLine(postResponse);
        }

This is my attempt to use Polly. It doesn't work as it still fails on around the same amount of requests as before.

What am I doing wrong?

var policy = Policy
              .Handle<HttpRequestException>()
              .RetryAsync(3);

foreach (var mediaItem in uploadedMedia)
{
    var mediaRequest = new HttpRequestMessage { *** }
    async Task<string> func()
    {
        var response = await client.SendAsync(mediaRequest);
        return await response.Content.ReadAsStringAsync();                            
    }                      
    tasks.Add(policy.ExecuteAsync(() => func()));                                              
}                                          
await Task.WhenAll(tasks);
Ryn901
  • 173
  • 2
  • 12
  • 3
    There are many ways, put them in a list to reprocess (don't use recursion), use Polly, Use dataflow and link back to it self (or Rx). Or any other wild and wooly scheme you can think of. Polly is built for exactly this kind of thing though – TheGeneral Oct 20 '20 at 06:56
  • Do you inject the client via Standard-DI? If so, have a look at this: [Implement HTTP call retries with exponential backoff with IHttpClientFactory and Polly policies](https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/implement-http-call-retries-exponential-backoff-polly) Should work with other DI Frameworks, too. – Fildor Oct 20 '20 at 07:09
  • Related: [How to use Task.WhenAny and implement retry](https://stackoverflow.com/questions/43763982/how-to-use-task-whenany-and-implement-retry) – Theodor Zoulias Oct 20 '20 at 07:59
  • "*Polly [...] it still fails on around the same amount of requests as before.*" <=== Can you be more specific? Does it fail be not retrying, or is it retrying successfully but all retries fail? In the second case you may be throttled by the server, for doing too many requests at a short time. – Theodor Zoulias Oct 21 '20 at 12:25
  • I think it is being throttled by the server. Is there a way to add a delay? e.g. a 1s delay after every 25 calls – Ryn901 Oct 25 '20 at 01:11

0 Answers0