![]() |
VOOZH | about |
dotnet add package RetryHelper --version 2.1.0
NuGet\Install-Package RetryHelper -Version 2.1.0
<PackageReference Include="RetryHelper" Version="2.1.0" />
<PackageVersion Include="RetryHelper" Version="2.1.0" />Directory.Packages.props
<PackageReference Include="RetryHelper" />Project file
paket add RetryHelper --version 2.1.0
#r "nuget: RetryHelper, 2.1.0"
#:package RetryHelper@2.1.0
#addin nuget:?package=RetryHelper&version=2.1.0Install as a Cake Addin
#tool nuget:?package=RetryHelper&version=2.1.0Install as a Cake Tool
This is a generic helper to help try some action until the given condition is met. It now works seamlessly with C# async/await keywords for asynchronous operations, which is very common in a scenario that requires retry logic.
Write retry logic for operations like web request or file operation in a more readable way rather than a try-catch nested in a loop. You can specify end conditions based on return value or exception, config the retry interval, maximum retry count and maximum retry time limitation.
using Retry;
RetryHelper.Instance.Try(() => TryDoSomething()).UntilNoException();
await RetryHelper.Instance.Try(async () => await TryGetValueAsync()).Until(async result => result < await GetQuota());
// Basic usage - keep trying every 500ms forever
RetryHelper.Instance.Try(() => TryGetValue()).Until(result => result < 0.1);
// Get the result from the retried method
var resultSmallEnough = RetryHelper.Instance.Try(() => TryGetValue()).Until(result => result < 0.1);
// Specify interval as 100 ms
RetryHelper.Instance.Try(() => TryGetValue()).WithTryInterval(100).Until(result => result < 0.1);
// Try 20 times maximum and throw TimeoutException if exceeded
RetryHelper.Instance.Try(() => TryGetValue()).WithMaxTryCount(20).Until(result => result < 0.1);
// Can also limit the total try time duration
RetryHelper.Instance.Try(() => TryGetValue()).WithTimeLimit(TimeSpan.FromSeconds(10)).Until(result => result < 0.1);
// Specify the extra success/fail/timeout action
RetryHelper.Instance.Try(() => TryGetValue())
.WithMaxTryCount(20)
.OnSuccess(result => Trace.TraceInformation($"Got result {result}."))
.OnFailure(result => Trace.TraceWarning($"Try failed. Got {result}."))
.OnTimeout(lastResult => Trace.TraceError("Did not get result under 0.1 in 20 times."))
.Until(result => result < 0.1);
OnSuccess: Executed after the condition is met.OnFailure: Executed after each failed attempt and before the next attempt.OnTimeout: Executed after all allowed attempts have failed.Multiple callbacks of the same type can be registered. In this case, the order of invocation is not guaranteed.
RetryHelper.Instance.Try(() => TryGetValue())
.OnFailure(result => Trace.TraceWarning($"Try failed. Got {result}."))
.OnFailure(() => Trace.TraceWarning($"As I said or will say, it failed."))
.Until(result => result < 0.1);
// Retry on any (non-fatal) exception
RetryHelper.Instance.Try(() => TryDoSomething()).UntilNoException();
// Retry on specific exception
RetryHelper.Instance.Try(() => TryDoSomething()).UntilNoException<ApplicationException>();
// Or pass the Type object as parameter
RetryHelper.Instance.Try(() => TryDoSomething()).UntilNoException(typeof(ApplicationException));
// Basic usage
await RetryHelper.Instance.Try(async () => await TryGetValueAsync()).Until(result => result < 0.1);
// async/await keywords can be omitted for simplicity in this case
await RetryHelper.Instance.Try(() => TryGetValueAsync()).Until(result => result < 0.1);
// Asynchronous until condition is also supported
await RetryHelper.Instance.Try(async () => await TryGetValueAsync()).Until(async result => result + await TryGetValueAsync() < 0.2);
// In case the operation is not asynchronous, but you want to use an asynchronous until condition, use TryAsync
await RetryHelper.Instance.TryAsync(() => TryGetValue()).Until(async result => result + await TryGetValueAsync() < 0.2);
// Asynchronous OnSuccess/OnFailure/OnTimeout
// Note that asynchronous operation taken in OnFailure counts against TimeLimit,
// i.e. when retrying with time limit, the more time taken in OnFailure, the less
// retries can be performed.
await RetryHelper.Instance.Try(async () => await TryGetValueAsync())
.WithTryInterval(100)
.WithMaxTryCount(20)
.OnSuccess(async result => await LogToServerAsync($"Got result {result}."))
.OnFailure(async result => await LogToServerAsync($"Try failed. Got {result}."))
.OnTimeout(async lastResult => await LogToServerAsync("Did not get result under 0.1 in 20 times."))
.Until(result => result < 0.1);
Just like the synchronous version above, multiple asynchronous callbacks of the same type can be registered. In this case, callbacks will be invoked and awaited one by one, although the order of invocation is not guaranteed. Asynchronous callbacks will not be invoked concurrently.
RetryHelper.Instance.DefaultMaxTryCount = 3;
RetryHelper.Instance.DefaultMaxTryTime = TimeSpan.FromSeconds(10);
RetryHelper.Instance.DefaultTryInterval = TimeSpan.FromMilliseconds(100);
var retryHelper = new RetryHelper(new TraceSource("MyTraceSource"))
{
DefaultMaxTryCount = 10,
DefaultMaxTryTime = TimeSpan.FromSeconds(30),
DefaultTryInterval = TimeSpan.FromMilliseconds(500),
};
UntilNoExceptionOnFailure, OnSuccess and OnTimeout callbacks to take no parameterThread.Sleep with Task.Delay for AsyncRetryTaskExtensions.MakeFunc obsolete which should not have been publicasync/await keywords)OnFailure is not respected if not registered last| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET Framework | net45 net45 is compatible. net451 net451 was computed. net452 net452 was computed. net46 net46 was computed. net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
This package has no dependencies.
Showing the top 1 NuGet packages that depend on RetryHelper:
| Package | Downloads |
|---|---|
|
WhiteSharx.BigQuery.HighLevelApi
Easy to use BigQuery connector |
This package is not used by any popular GitHub repositories.
Support async/await.