EasyCore.Polly 8.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasyCore.Polly --version 8.0.1
                    
NuGet\Install-Package EasyCore.Polly -Version 8.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="EasyCore.Polly" Version="8.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasyCore.Polly" Version="8.0.1" />
                    
Directory.Packages.props
<PackageReference Include="EasyCore.Polly" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EasyCore.Polly --version 8.0.1
                    
#r "nuget: EasyCore.Polly, 8.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.
#:package EasyCore.Polly@8.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EasyCore.Polly&version=8.0.1
                    
Install as a Cake Addin
#tool nuget:?package=EasyCore.Polly&version=8.0.1
                    
Install as a Cake Tool

πŸ›‘οΈ EasyCore.Polly

EasyCore.Polly is a production-ready Polly resilience wrapper for .NET 8. Apply retry, timeout, circuit breaker, bulkhead, and fallback with [PollyConfig] on interface methods β€” powered by Castle DynamicProxy.

<p align="center"> <img src="https://raw.githubusercontent.com/RockyWang0521/EasyCore.Polly/master/png/EasyCoreLogo.png" alt="EasyCore Logo" width="120" /> </p>

.NET C# Polly Proxy License Version


🌍 Language


πŸ“š Table of Contents

Part I β€” Overview & Architecture

Part II β€” Getting Started

Part III β€” Policies & Production


1. 🎯 Positioning

EasyCore.Polly solves β€œdeclare resilience with attributes instead of hand-written Polly wraps”:

Pain point EasyCore.Polly approach
Hand-written Polly wraps everywhere [PollyConfig] on methods
Manual proxy registration EasyCorePolly + interface discovery
Fragile filesystem scanning Prefer AddAssemblyFrom<T>()
Broken Task<T> fallback Generic policies return degradation values
Library forces console logging Host owns logging configuration

1.1 Design Principles

Principle Meaning
Low friction One extension method + one attribute to get running
Interface proxy Castle DynamicProxy β€” transparent to callers
Explicit assemblies Use AddAssemblyFrom<T>() in production
Composable policies Retry / Timeout / CB / Bulkhead / Fallback in one attribute
Clean namespace Folders for organization; public API stays EasyCore.Polly

2. πŸ—οΈ Architecture & Call Chain

2.1 Component diagram

architecture-en

2.2 Call sequence

[IOrderClient] ──► Castle Interface Proxy
                         β”‚
                         β–Ό
              PollyStandardInterceptor
                         β”‚
                         β–Ό
         PolicyCache ──► PolicyFactory
                         β”‚
                         β–Ό
              Fallback β†’ Retry β†’ Timeout
              β†’ CircuitBreaker β†’ Bulkhead
                         β”‚
                         β–Ό
                 OrderClient.Method()

3. πŸ“ Repository Layout

EasyCore.Polly/
β”œβ”€β”€ src/EasyCore.Polly/
β”‚   β”œβ”€β”€ Abstractions/     # Options, Attribute
β”‚   β”œβ”€β”€ Discovery/       # Assembly discovery
β”‚   β”œβ”€β”€ Hosting/         # DI extension entry
β”‚   β”œβ”€β”€ Interception/    # Castle interceptor
β”‚   └── Policies/        # Policy factory and cache
β”œβ”€β”€ demo/
β”‚   β”œβ”€β”€ PollyTest/                  # Shared sample services
β”‚   β”œβ”€β”€ WebApp.EasyCorePolly/       # ASP.NET Core + Swagger
β”‚   └── WinFormsApp.EasyCorePolly/  # WinForms
β”œβ”€β”€ tests/EasyCore.Polly.Tests/
β”œβ”€β”€ docs/svg/                       # README diagrams
└── png/EasyCoreLogo.png

Folders are for organization only; the public namespace stays EasyCore.Polly.


4. πŸ“¦ Installation

dotnet add package EasyCore.Polly

Requires .NET 8.


5. ⚑ Quick Start

5.1 Define an interface and implementation

using EasyCore.Polly;

public interface IOrderClient
{
    Task<string> GetOrderAsync(string id);
}

public sealed class OrderClient : IOrderClient
{
    [PollyConfig(
        MaxRetry = 3,
        RetryIntervalSeconds = 1,
        TimeOutSeconds = 5,
        IsEnableCircuitBreaker = true,
        ExceptionsAllowedBeforeBreaking = 5,
        SecondsOfBreak = 30,
        ServiceDegradation = nameof(GetOrderFallbackAsync))]
    public Task<string> GetOrderAsync(string id)
        => throw new HttpRequestException("upstream down");

    public Task<string> GetOrderFallbackAsync()
        => Task.FromResult("cached-or-default");
}

Interface proxy is required. The implementation type name OrderClient pairs with interface IOrderClient.

5.2 Register services

Either style works:

// Convenient: auto-scan candidate assemblies (demo / local)
builder.Services.EasyCorePolly();

// Production: scan an explicit assembly
builder.Services.EasyCorePolly(options =>
{
    options.AddAssemblyFrom<OrderClient>();
});

Resolve IOrderClient from DI β€” you get a Castle proxy that applies policies.


6. βš™οΈ Attributes & Options

6.1 [PollyConfig] property reference

Property Meaning Default Icon
RateLimit Bulkhead max parallelization (0 = off). Not a per-second rate limiter. 0 🧱
RateQueuing Bulkhead max queued actions 0 πŸ“₯
MaxRetry Retry count (0 = off) 0 πŸ”
RetryIntervalSeconds Delay between retries 1 ⏱️
IsEnableCircuitBreaker Enable circuit breaker false ⚑
ExceptionsAllowedBeforeBreaking Failures before open 3 πŸ’₯
SecondsOfBreak Open duration (seconds) 5 ⏸️
TimeOutSeconds Timeout (0 = off) 0 βŒ›
ServiceDegradation Parameterless fallback method name on the same type null 🩹

6.2 EasyCorePollyOptions

API Meaning
AddAssemblyFrom<T>() Scan the assembly that contains T (production recommended)
AddAssembly(Assembly) Scan an explicit assembly
Assemblies Configured assembly list (empty β‡’ candidate scan)

7. 🧩 Policy Wrap Order

7.1 Visual wrap

policy-wrap-en

7.2 Execution order

Outermost β†’ innermost:

Fallback β†’ Retry β†’ Timeout β†’ CircuitBreaker β†’ Bulkhead β†’ method

This matches Polly WrapAsync composition used by EasyCore.Polly.

Policy Role Property
🩹 Fallback Degrade on final failure ServiceDegradation
πŸ” Retry Transient fault retries MaxRetry / RetryIntervalSeconds
βŒ› Timeout Prevent long hangs TimeOutSeconds
⚑ CircuitBreaker Stop cascading failures IsEnableCircuitBreaker, etc.
🧱 Bulkhead Cap concurrency / isolate faults RateLimit / RateQueuing

8. πŸ§ͺ Demo Projects

Project Description Scenario
demo/WebApp.EasyCorePolly ASP.NET Core + Swagger Web API sample
demo/WinFormsApp.EasyCorePolly WinForms Desktop sample
demo/PollyTest Shared [PollyConfig] services Referenced by demos above
dotnet test tests/EasyCore.Polly.Tests
dotnet run --project demo/WebApp.EasyCorePolly

9. βœ… Production Checklist

  • Call AddAssemblyFrom<T>() (do not rely on base-directory DLL scanning)
  • Expose services through interfaces (I{ClassName})
  • Configure logging in the host (console/file/OTLP) β€” the library does not add sinks
  • Tune circuit-breaker and timeout values for your SLOs
  • Prefer returning values from degradation methods for Task<T> APIs
  • Invalid [PollyConfig] values throw at first execution

10. ❓ FAQ

Q: Why must I use an interface?
A: Policies are applied via Castle interface proxies. Class-only registration is not proxied.

Q: Is RateLimit a per-second throttle?
A: No. It maps to Polly Bulkhead (max concurrent executions + optional queue). See the wrap diagram above.

Q: What can a fallback method return?
A: Task, Task<T>, or a synchronous T for generic methods. It must be parameterless and on the same type.

Q: How does this relate to Ocelot / API gateways?
A: EasyCore.Polly is in-process resilience. Gateways handle edge traffic; use both when appropriate.

Q: Polly v8?
A: This package targets Polly 7.2.x so the attribute model stays stable. Polly v8 pipelines are a separate migration.


11. πŸ“„ License

MIT

🀝 Contributing

  1. Fork and create a feature branch
  2. Add tests under tests/EasyCore.Polly.Tests
  3. Run dotnet test and dotnet build EasyCore.Polly.sln
  4. Open a pull request

Issues / PRs welcome πŸš€

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.