Polly: How to use retry and fallback together?

Created on 1 Dec 2017  路  2Comments  路  Source: App-vNext/Polly

I would like to try an action three times and if it fails then call fallback... For example something like this:

Policy.Handle()
.WaitAndRetry(retryCount: 7, sleepDurationProvider: (attempt) => { return TimeSpan.FromSeconds(value: Math.Pow(attempt, 2)); })
.Execute(action: () => {
this.DoTheMagic();
}).
Fallback(()=>{ this.LogThatMagicWasNotDone();}
;

but it does not work. How to achieve such behavior? Or do I have to insert whole Polly call into my own try-catch statement?
Additionaly, am I able to somehow inject code after every failed attempt? So that I can log for example "Attempt number X: failed"?

Thanks!

question

Most helpful comment

@romanb52

To combine policies, see PolicyWrap.

To run code after each failure, use the overloads of WaitAndRetry(...) where you can configure an onRetry: delegate - see readme

Here's an example I wrote a while back in dotnetfiddle.

All 2 comments

@romanb52

To combine policies, see PolicyWrap.

To run code after each failure, use the overloads of WaitAndRetry(...) where you can configure an onRetry: delegate - see readme

Here's an example I wrote a while back in dotnetfiddle.

Closing. If you need further assistance, though, do open another issue - or post a query on our slack channel

Was this page helpful?
0 / 5 - 0 ratings