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);
5
  • 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 Commented Oct 20, 2020 at 6: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 Should work with other DI Frameworks, too. Commented Oct 20, 2020 at 7:09
  • Related: How to use Task.WhenAny and implement retry Commented Oct 20, 2020 at 7: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. Commented Oct 21, 2020 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 Commented Oct 25, 2020 at 1:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.