Http.Resilience
2.0.19-pre
See the version list below for details.
dotnet add package Http.Resilience --version 2.0.19-pre
NuGet\Install-Package Http.Resilience -Version 2.0.19-pre
<PackageReference Include="Http.Resilience" Version="2.0.19-pre" />
paket add Http.Resilience --version 2.0.19-pre
#r "nuget: Http.Resilience, 2.0.19-pre"
// Install Http.Resilience as a Cake Addin #addin nuget:?package=Http.Resilience&version=2.0.19-pre&prerelease // Install Http.Resilience as a Cake Tool #tool nuget:?package=Http.Resilience&version=2.0.19-pre&prerelease
Http.Resilience
Http.Resilience adds fault tolerance to any HTTP request. It can be used together with C# HttpClient, WebRequest or any other HTTP client. Transient network failures are automatically catched and a configurable number of retries is issued.
Download and Install Http.Resilience
This library is available on NuGet: https://www.nuget.org/packages/Http.Resilience Use the following command to install Http.Resilience using NuGet package manager console:
PM> Install-Package Http.Resilience
You can use this library in any .NET Standard or .NET Core project.
API Usage
HttpRetryHelper
provides easy-to-use Invoke
and InvokeAsync
methods which support configurable retry behavior - not only for the wellknown HttpClient but also for other http clients such as WebRequest or WebClient.
HttpRetryHelper
is equipped with some good-practice retry logic but it also exposes configurability via HttpRetryOptions
.
Recover from transient network failure
The following sample demonstrates a simple HTTP request using HttpClient. HttpRetryHelper is used to wrap httpClient.GetAsync(...). Whenever GetAsync(...) fails due to a transilient network failure, HttpRetryHelper attempts to recover the problem by repeatedly calling InvokeAsync.
var httpClient = new HttpClient();
var requestUri = "https://quotes.rest/qod?language=en";
var httpRetryHelper = new HttpRetryHelper(maxRetries: 3);
try
{
var httpResponseMessage = await httpRetryHelper.InvokeAsync(async () => await httpClient.GetAsync(requestUri));
var jsonContent = await httpResponseMessage.Content.ReadAsStringAsync();
Console.WriteLine($"{jsonContent}");
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
Recover from unsuccessful HTTP status code
Retries can be configured using the RetryOnException delegate. If Invoke/Async throws an exception, we can intercept it with RetryOnException((ex) ⇒ ...) and return a bool value to indicate whether we want to retry the particular HTTP request (true=retry, false=do not retry).
var httpClient = new HttpClient();
var requestUri = "https://quotes.rest/qod?language=en";
var httpRetryOptions = new HttpRetryOptions();
httpRetryOptions.MaxRetries = 4;
var httpRetryHelper = new HttpRetryHelper(httpRetryOptions);
httpRetryHelper.RetryOnException<HttpRequestException>(ex => { return ex.StatusCode == HttpStatusCode.ServiceUnavailable; });
try
{
var httpResponseMessage = await httpRetryHelper.InvokeAsync(async () => await httpClient.GetAsync(requestUri));
var jsonContent = await httpResponseMessage.Content.ReadAsStringAsync();
Console.WriteLine($"{jsonContent}");
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
Retry based on returned result
Retries can also be carried out if a particular result is returned by Invoke/InvokeAsync. RetryOnResult delegate allows to evaluate the returned result and indicate if a retry is necessary (true=retry, false=do not retry).
var httpClient = new HttpClient();
var requestUri = "https://yourapi/token/refresh";
var httpRetryOptions = new HttpRetryOptions();
httpRetryOptions.MaxRetries = 4;
var httpRetryHelper = new HttpRetryHelper(httpRetryOptions);
httpRetryHelper.RetryOnResult<RefreshTokenResult>(r => r.Error != "invalid_grant");
try
{
var refreshTokenResult = await httpRetryHelper.InvokeAsync(async () => await httpClient.PostAsync(requestUri, ...));
// ...
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
License
This project is Copyright © 2023 Thomas Galliker. Free for non-commercial use. For commercial use please contact the author.
Product | Versions 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 is compatible. |
.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. |
-
.NETStandard 2.0
- Microsoft.Extensions.Configuration.Abstractions (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Options (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
-
.NETStandard 2.1
- Microsoft.Extensions.Configuration.Abstractions (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Options (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
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 |
---|---|---|
2.1.2 | 100 | 12/2/2024 |
2.0.19-pre | 58 | 12/2/2024 |
2.0.18-pre | 91 | 11/5/2024 |
2.0.17-pre | 59 | 11/5/2024 |
2.0.15-pre | 84 | 10/24/2024 |
2.0.13 | 401 | 6/17/2024 |
2.0.12 | 1,096 | 3/29/2024 |
2.0.11-pre | 93 | 3/27/2024 |
2.0.10-pre | 96 | 3/11/2024 |
2.0.9 | 5,402 | 10/6/2023 |
2.0.8-pre | 133 | 10/3/2023 |
2.0.6-pre | 116 | 10/3/2023 |
2.0.4-pre | 138 | 9/23/2023 |
2.0.3-pre | 118 | 9/23/2023 |
2.0.0-pre | 117 | 9/23/2023 |
1.4.6 | 410 | 9/1/2023 |
1.4.4-pre | 119 | 9/1/2023 |
1.4.2 | 148 | 9/1/2023 |
1.4.1-pre | 122 | 9/1/2023 |
1.4.0-pre | 129 | 8/31/2023 |
1.3.13-pre | 130 | 8/31/2023 |
1.3.12-pre | 128 | 8/31/2023 |
1.3.11-pre | 119 | 8/31/2023 |
1.3.10 | 202 | 6/20/2023 |
1.3.8-pre | 134 | 6/20/2023 |
1.3.2-pre | 173 | 12/31/2022 |
1.3.1-pre | 169 | 12/12/2022 |
1.3.0-pre | 159 | 12/12/2022 |
1.2.13 | 438 | 12/9/2022 |
1.2.12-pre | 165 | 12/9/2022 |
1.2.11 | 333 | 11/24/2022 |
1.2.9-pre | 174 | 11/24/2022 |
1.2.7-pre | 168 | 11/24/2022 |
1.1.34 | 14,296 | 6/17/2022 |
1.1.33-pre | 191 | 6/17/2022 |
1.1.31-pre | 176 | 6/17/2022 |
1.1.28-pre | 211 | 5/5/2022 |
1.1.27-pre | 189 | 4/27/2022 |
1.1.26-pre | 182 | 4/26/2022 |
1.1.25 | 447 | 4/26/2022 |
1.1.23-pre | 193 | 4/26/2022 |
1.1.18-pre | 198 | 4/26/2022 |
1.1.14-pre | 192 | 4/25/2022 |
1.1.10-pre | 211 | 4/21/2022 |
1.1.4-pre | 190 | 4/21/2022 |
1.1.0-pre | 200 | 4/21/2022 |
1.0.36 | 421 | 4/21/2022 |
1.0.31-pre | 217 | 1/9/2022 |
1.0.29-pre | 194 | 12/26/2021 |
1.0.28 | 303 | 12/21/2021 |
1.0.26-pre | 201 | 12/21/2021 |
1.0.25 | 292 | 12/18/2021 |
1.0.23-pre | 227 | 12/18/2021 |
1.0.15-pre | 213 | 12/13/2021 |
1.0.6-pre | 219 | 12/9/2021 |
1.0.4-pre | 194 | 12/9/2021 |
1.0.2-pre | 234 | 12/8/2021 |
1.0.0-pre | 208 | 12/8/2021 |
2.0
- Integrate Microsoft.Extensions.Logging
- Integrate Microsoft.Extensions.DependencyInjection
- Integrate Microsoft.Extensions.Options
1.4
- Improve log messages
1.3
- Minimum .NET framework version is now 4.8
- Code cleanup
1.2
- Cleanups
1.1
- Support retry policies for Exceptions and HttpResponseMessages
- Add new retry handler RetryOnResult
- Code cleanup
1.0
- Initial release