Skip to content

tongwu-sh/SK.RetryLib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RetryLib

RetryLib is a .Net Library for C# Retry Operations. It allows developer to write retry code easily for Web/IO Trasient or custom error. It also supports async method.

Installing via NuGet

Install-Package SK.RetryLib

Sample code

###Retry Action or Function### Retry action or function if there's some exceptions.

// Retry at most 3 times
Retry.Action(() =>
             {
                // Do something here.
             }, 3);
                
// Retry at most 3 times & wait 2 sec
var result = Retry.Func(() =>
             {
                // Do something here.
                return XXX;
             }, 3, 2 * 1000);

###Retry async method###

var task = Retry.FuncAsync(
                async () =>
                {
                    WebRequest request = WebRequest.Create(@"http://www.bing.com/");
                    return await request.GetResponseAsync();
                }, 3);
task.Wait();

###Use Retry Policy### Retry Policy is used for only retry specific Exception. WebRetryPolicy and IORetryPolicy are provided by RetryLib, and developer can implement IRetryPolicy for custom use.

var task = Retry.FuncAsync(
                async () =>
                {
                    WebRequest request = WebRequest.Create(@"http://www.bing.com/");
                    return await request.GetResponseAsync();
                }, 3, retryPolicy: new WebRetryPolicy()); // Pass Retry Policy to task.
task.Wait();

###Retry Wait Type###

  • Linear: wait Xs, Xs, Xs....
  • Double: wait Xs, 2Xs, 4Xs....
  • Random: wait (0, Xs)
  • Zero: never wait
// Wait 2 sec, 4 sec, 8 sec...
Retry.Func(() =>
            {
                // Do something here.
                return XXX;
            }, 4, 2 * 1000, waitType: RetryWaitType.Double);

###Exception Handler### RetryLib provides event OnExceptionCatch to handle Exceptions in function.

Retry.Func(() =>
            {
                // Do something here.
                return XXX;
            }, 4, 2 * 1000, 
            exceptionHandler: (sender, exceptionArgs) => 
            {
                // Do something in exception handler
                record(exceptionArgs.Ex);
            });

Releases

No releases published

Packages

No packages published

Languages