Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make conditional WaitAndRetry or WaitAndRetryForever depending on the exception?

Tags:

c#

asp.net

polly

Here is my try:

private Policy retryPolicy { get; } = Policy
    .Handle<IOException>()
    .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
private Policy bigFilePolicy { get; } = Policy
    .Handle<UnauthorizedAccessException>()
    .WaitAndRetryForeverAsync(retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));

retryPolicy.ExecuteAsync(() => bigFilePolicy.ExecuteAsync(command));  

I am trying to make a retry policy for an automatic file upload system. When the file is too big, it takes some time to be dropped in the folder and while it loads, my program gives an unauthorized access exception, in such cases I am thinking of allowing WaitAndRetryForever for the big files. Otherwise, if it gets other exceptions it should not retry forever.

like image 483
Frying Pan Avatar asked Dec 18 '25 21:12

Frying Pan


1 Answers

To mix policies together you need to call Policy.Wrap/Policy.WrapAsync. For example, you could do:

var exponentialBackoffPolicy = Policy.WrapAsync(retryPolicy, bigFilePolicy);

Or even:

var exponentialBackoffPolicy = retryPolicy.WrapAsync(bigFilePolicy);
like image 63
DavidG Avatar answered Dec 20 '25 11:12

DavidG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!