Sander.MultiTry 1.0.1

dotnet add package Sander.MultiTry --version 1.0.1                
NuGet\Install-Package Sander.MultiTry -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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                
#r "nuget: Sander.MultiTry, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Sander.MultiTry as a Cake Addin
#addin nuget:?package=Sander.MultiTry&version=1.0.1

// Install Sander.MultiTry as a Cake Tool
#tool nuget:?package=Sander.MultiTry&version=1.0.1                

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(() => 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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 1,068 4/16/2018
1.0.0 897 4/10/2018