Wrapper around Polly Framework so that implementation can stay at a single place

Dev*_*per 1 c# oop polly

I have gone through the documentation and examples of Polly Framework, and it's really awesome and simple to use !!

In my case, I want to classify all the exceptions into 3 types: temporary, permanent and log. Now, I want to have a single piece of code which will be responsible to handle errors which are temporary in nature by doing wait and retry using Polly Framework.

  WaitAndRetryAsync(new[]{
                    TimeSpan.FromSeconds(1),
                    TimeSpan.FromSeconds(2),
                    TimeSpan.FromSeconds(5)
                  })
Run Code Online (Sandbox Code Playgroud)

Same way, if something is permanent in nature (should be able to know it's type based on the exception we are trying to handle example: timeout can be interim but if database cred are not correct then it's permanent) then we can send email to the support staff.

Now, the problem starts here that, how I can wrap it using an interface or Abstract class so that my all the derived class can pass on an exception along with method name, which can be passed on to my new framework and it will do the needful.

Any pointers will be of great help !

Scr*_*obi 5

您可以创建一个类来接受您希望 Polly 调用的代码的FuncAction

像这样的辅助方法:

public class RetryWrapper
    {
        public static TOutput Execute<TInput, TOutput>(Func<TInput, TOutput> func, TInput input)
        {
            RetryPolicy retryPolicy = Policy.Handle<TimeoutException>()
                .Or<OtherException>()
                .WaitAndRetry(3, x => new TimeSpan(0, 0, 2));

            return retryPolicy.Execute(() => func(input));
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后你可以调用它

var result = RetryWrapper.Execute<string, bool>(x => MethodToCall(x), "input")
Run Code Online (Sandbox Code Playgroud)