Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Polly C# retry exponentially for 3 tries then switch to retrying every hour

I am trying to use polly to construct a policy that can retry exponentially for n tries and then switch to retrying every 1 hour. Can this be achieved?

I tried policy Wrap but did not get the desired results

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This should be possible to achieve with overloads accepting custom sleepDurationProvider. For example something like the following:

var waitAndRetry = Policy
    .Handle<SomeException>()
    .WaitAndRetry(4, retryAttempt => retryAttempt switch // or WaitAndRetryForever
    {
        <= 3 => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), // for example n == 3
        _ => TimeSpan.FromHours(1)
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading