Sander.MultiTry 1.0.1

MultiTry

There are great many operations which can have transient errors, such as network requests, database operations and many more. In case of a transient exception, the operation usually needs to be retried.

MultiTry is created to reduce the amount of boilerplate code that needs to be written for such operations. The goal was to have an easy-to-use, but yet flexible and powerful re-try mechanism, which supports exception filtering, handling and re-throwing.

Install-Package Sander.MultiTry -Version 1.0.1
dotnet add package Sander.MultiTry --version 1.0.1
<PackageReference Include="Sander.MultiTry" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sander.MultiTry --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

There are great many operations which can have transient errors, such as network requests, database operations and many more. In case of a transient exception, the operation usually needs to be retried.

MultiTry is created to reduce the amount of boilerplate code that needs to be written for such operations. The goal was to have an easy-to-use, but yet flexible and powerful re-try mechanism, which supports exception filtering, handling and re-throwing - see examples below.

Features

  • Easy to use
  • Supports both functions (returns result) and actions (no return value)
  • Supports async functions
  • Optional exception filtering
  • Configurable number of retries
  • Configurable delay between attempts
  • .NET Standard 2.0, meaning MultiTry can be used with .NET Framework 4.6.1+, .NET Core 2.0 and more - see here for detailed information.

Examples

Simplest possible use case:
UserInfo userInfo = MultiTry.Try(() =&gt; GetUserInfo(userId))

This executes method GetUserInfo() with the default settings, meaning:

  • 3 attempts to execute GetUserInfo()
  • No delay between the attempts
  • If all retries get an exception, Try returns default(UserInfo) (null if UserInfo is a class).

A more complex example with exception filtering and logging:

var options = MultiTryOptions<UserInfo>.Default;

//delay 1000 ms between attempts
options.Delay = 1000;

//try up to five times
options.TryCount = 5;

//retry only if the exception is SqlException
options.ExceptionFilter = ex => ex.GetType() == typeof(SqlException);

//log exception information
options.OnException = (ex, i) =>
{
	Trace.WriteLine($"Attempt {i}: {ex}");
	return true; //continue attempts	
};

//if all retries fail, create a new user and return that
options.OnFinalFailure = ex =>
{
	Trace.WriteLine($"Creating a new user with id {userId}. Fetching user failed with exception {ex}.";
	return new UserInfo { UserId = userId, IsNew = true }; 
};

var userInfo = MultiTry.Try(() => GetUserInfo(userId), options);

There is also a full commented example in tests, FullExample.cs.

There are great many operations which can have transient errors, such as network requests, database operations and many more. In case of a transient exception, the operation usually needs to be retried.

MultiTry is created to reduce the amount of boilerplate code that needs to be written for such operations. The goal was to have an easy-to-use, but yet flexible and powerful re-try mechanism, which supports exception filtering, handling and re-throwing - see examples below.

Features

  • Easy to use
  • Supports both functions (returns result) and actions (no return value)
  • Supports async functions
  • Optional exception filtering
  • Configurable number of retries
  • Configurable delay between attempts
  • .NET Standard 2.0, meaning MultiTry can be used with .NET Framework 4.6.1+, .NET Core 2.0 and more - see here for detailed information.

Examples

Simplest possible use case:
UserInfo userInfo = MultiTry.Try(() =&gt; GetUserInfo(userId))

This executes method GetUserInfo() with the default settings, meaning:

  • 3 attempts to execute GetUserInfo()
  • No delay between the attempts
  • If all retries get an exception, Try returns default(UserInfo) (null if UserInfo is a class).

A more complex example with exception filtering and logging:

var options = MultiTryOptions<UserInfo>.Default;

//delay 1000 ms between attempts
options.Delay = 1000;

//try up to five times
options.TryCount = 5;

//retry only if the exception is SqlException
options.ExceptionFilter = ex => ex.GetType() == typeof(SqlException);

//log exception information
options.OnException = (ex, i) =>
{
	Trace.WriteLine($"Attempt {i}: {ex}");
	return true; //continue attempts	
};

//if all retries fail, create a new user and return that
options.OnFinalFailure = ex =>
{
	Trace.WriteLine($"Creating a new user with id {userId}. Fetching user failed with exception {ex}.";
	return new UserInfo { UserId = userId, IsNew = true }; 
};

var userInfo = MultiTry.Try(() => GetUserInfo(userId), options);

There is also a full commented example in tests, FullExample.cs.

  • .NETStandard 2.0

    • No dependencies.

Version History

Version Downloads Last updated
1.0.1 231 4/16/2018
1.0.0 192 4/10/2018