项目作者: Dixin

项目描述 :
Enterprise Library - Transient Fault Handling Application Block for .NET Core / .NET Standard, with improvement.
高级语言: C#
项目地址: git://github.com/Dixin/EnterpriseLibrary.TransientFaultHandling.Core.git
创建时间: 2016-12-19T03:33:23Z
项目社区:https://github.com/Dixin/EnterpriseLibrary.TransientFaultHandling.Core

开源协议:MIT License

下载


EnterpriseLibrary.TransientFaultHandling.Core

Transient Fault Handling Application Block/Retry patterns for modern .NET.

Build status

TransientFaultHandling.Core is retry library for transient error handling. It is ported from Microsoft Enterprise Library’s Transient Fault Handling Application Block, a library widely used with .NET Framework.

  • The retry pattern APIs are ported to .NET 6 & .NET 5 & .NET Core & .NET Standard.
  • New functional and fluent APIs to easily implement retry logic.
  • The outdated configuration & APIs are replaced by modern .NET JSON/XML/INI configuration.

Introduction

With this library, the old code with retry logic based on Microsoft Enterprise Library can be ported to .NET 6 & .NET 5 & .NET Core & .NET Standard without modification:

  1. ITransientErrorDetectionStrategy transientExceptionDetection = new MyDetection();
  2. RetryStrategy retryStrategy = new FixedInterval(retryCount: 5, retryInterval: TimeSpan.FromSeconds(1));
  3. RetryPolicy retryPolicy = new RetryPolicy(transientExceptionDetection, retryStrategy);
  4. retryPolicy.ExecuteAction(() => webClient.DownloadString("https://DixinYan.com"));

With this library, it is extremely easy to detect transient exception and implement retry logic. For example, the following code downloads a string, if the exception thrown is transient (a WebException), it retries up to 5 times, with it waits for 1 second between each retry:

  1. Retry.FixedInterval(
  2. () => webClient.DownloadString("https://DixinYan.com"),
  3. isTransient: exception => exception is WebException,
  4. retryCount: 5, retryInterval: TimeSpan.FromSeconds(1));

Fluent APIs are also provided for even better readability:

  1. Retry
  2. .WithIncremental(retryCount: 5, initialInterval: TimeSpan.FromSeconds(1),
  3. increment: TimeSpan.FromSeconds(1))
  4. .Catch<OperationCanceledException>()
  5. .Catch<WebException>(exception =>
  6. exception.Response is HttpWebResponse { StatusCode: HttpStatusCode.RequestTimeout })
  7. .ExecuteAction(() => webClient.DownloadString("https://DixinYan.com"));

It also supports JSON/XML/INI configuration:

  1. {
  2. "retryStrategy": {
  3. "name1": {
  4. "fastFirstRetry": "true",
  5. "retryCount": 5,
  6. "retryInterval": "00:00:00.1"
  7. },
  8. "name2": {
  9. "fastFirstRetry": "true",
  10. "retryCount": 55,
  11. "initialInterval": "00:00:00.2",
  12. "increment": "00:00:00.3"
  13. }
  14. }
  15. }

Document

https://weblogs.asp.net/dixin/transientfaulthandling-core-retry-library-for-net-core-net-standard

Source

https://github.com/Dixin/EnterpriseLibrary.TransientFaultHandling.Core (Partially ported from https://github.com/MicrosoftArchive/transient-fault-handling-application-block, with additional new APIs and redesigned/reimplemented APIs)

NuGet installation

It can be installed through NuGet using .NET CLI:

  1. dotnet add package EnterpriseLibrary.TransientFaultHandling.Core
  2. dotnet add package TransientFaultHandling.Caching
  3. dotnet add package TransientFaultHandling.Configuration
  4. dotnet add package TransientFaultHandling.Data

Or in Visual Studio NuGet Package Manager Console:

  1. Install-Package EnterpriseLibrary.TransientFaultHandling.Core
  2. Install-Package TransientFaultHandling.Caching
  3. Install-Package TransientFaultHandling.Configuration
  4. Install-Package TransientFaultHandling.Data