Search code examples
c#.netpollyretry-logicpolicywrap

Execute multiple policies


How to execute multiple policies (or combine them into a single one)?

For example I have:

var policy1 = Policy.Handle<DivideByZeroException>().WaitAndRetry(5));

var policy2 = Policy.Handle<StackOverflowException>().RetryForever();

How to apply them to one method at the same time?


Solution

  • As of Polly v5.0, there's a new PolicyWrap class, which let you combine multiple policies.

    var policy1 = Policy.Handle<DivideByZeroException>().WaitAndRetry(3, i => TimeSpan.FromSeconds(1));
    var policy2 = Policy.Handle<StackOverflowException>().RetryForever();
    PolicyWrap policyWrap = Policy.Wrap(policy1, policy2);
    policyWrap.Execute(someGreatAction);