Galosys.Foundation.Polly.Core 26.9.16.1

dotnet add package Galosys.Foundation.Polly.Core --version 26.9.16.1
                    
NuGet\Install-Package Galosys.Foundation.Polly.Core -Version 26.9.16.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="Galosys.Foundation.Polly.Core" Version="26.9.16.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.Polly.Core" Version="26.9.16.1" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.Polly.Core" />
                    
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 Galosys.Foundation.Polly.Core --version 26.9.16.1
                    
#r "nuget: Galosys.Foundation.Polly.Core, 26.9.16.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 Galosys.Foundation.Polly.Core@26.9.16.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=Galosys.Foundation.Polly.Core&version=26.9.16.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.Polly.Core&version=26.9.16.1
                    
Install as a Cake Tool

Galosys.Foundation.Polly.Core

成熟度: 🟡 活跃开发 — 可用,仍在演进

Galosys.Foundation.Polly.Core 提供弹性管道的 Polly 真实实现(v8),与 Galosys.Foundation.Core 中的抽象层(IResiliencePipeline / IResiliencePipelineBuilder / IResiliencePipelineProvider)解耦。

模块职责分离:Core 只定义抽象 + NoOp 占位(AddResilienceCore());本模块通过 AddPollyResilience() 覆盖 NoOp,启用真实的 Polly 策略。

依赖反转:本模块依赖 Galosys.Foundation.Castle.Core,以提供 [Retryable] AOP 拦截器(Castle.DynamicProxy.RetryableAttribute)。Castle.Core 本身不依赖本模块,保留纯 AOP 职责。

安装

<PackageReference Include="Galosys.Foundation.Polly.Core" />

依赖:polly.core 8.4.2、polly.ratelimiting 8.4.2、Galosys.Foundation.Castle.Core(提供 [Retryable] AOP 拦截器;中央包管理已配置)。

最小化配置

services.AddCore();             // Core 注册(内置 AddResilienceCore() NoOp)
services.AddPollyResilience();  // 本模块:覆盖 NoOp,启用 Polly 实现

自动注入(模块化宿主)

PollyCoreModule 在宿主调用 UseModularization() 时自动执行以下动作,无需显式调用

  1. 注册默认 retry 命名管道(RetryableAttribute.PipelineName,含 MaxRetries = 3 + Exponential Backoff)
  2. 调用 AddPollyResilience() 覆盖 Core NoOp
Host.CreateDefaultBuilder(args)
    .UseModularization()   // 自动发现并加载 CastleModule + PollyCoreModule(顺序由 DependsOn 控制)
    .Locate();
  • 仅需引用本程序集即自动启用真实 Polly 实现 + 默认 retry 命名管道。
  • 非模块化宿主(如 WPF 直连 AddCore)仍需显式 services.AddPollyResilience()
  • 可通过模块过滤配置禁用:ModuleFilterOptionsDisabled 加入 PollyCoreModule

使用方式

注册命名管道

services.AddResilienceCore("order-api", builder => builder
    .WithRetry(new RetryOptions { MaxRetries = 3, Delay = TimeSpan.FromMilliseconds(200) })
    .WithCircuitBreaker(new CircuitBreakerOptions { FailureRatio = 0.5, MinimumThroughput = 10 })
    .WithTimeout(new TimeoutOptions { Timeout = TimeSpan.FromSeconds(5) }));
services.AddPollyResilience();

注入并使用

public class OrderService(IResiliencePipelineProvider provider)
{
    private readonly IResiliencePipeline _pipeline = provider.GetPipeline("order-api");

    public async Task<Order> GetOrderAsync(string id, CancellationToken ct = default)
        => await _pipeline.ExecuteAsync(async token => await _repository.FindAsync(id, token), ct);
}

架构说明

工厂模式解耦(IResiliencePipelineBuilderFactory

PollyResiliencePipelineProvider 通过 IResiliencePipelineBuilderFactory 创建 Builder,而非 static abstract Create()(当时会导致 CS8920 泛型约束错误):

组件 模块 职责
IResiliencePipelineBuilderFactory Core 工厂接口
NoOpResiliencePipelineBuilderFactory Core NoOp Builder 工厂
PollyResiliencePipelineBuilderFactory 本模块 Polly Builder 工厂

DI 注册

  • AddResilienceCore()TryAddSingleton + AddSingleton(NoOp 首次注册)
  • AddPollyResilience()AddSingleton TryAdd,覆盖 NoOp 注册)

策略实现

策略 实现
Retry / CircuitBreaker / Timeout / RateLimiter Polly v8 原生
Bulkhead(舱壁) SemaphoreSlim 自实现(Polly v8 无内置)
Fallback(降级) Polly ExecuteAsync<T>

下游模块接入示例

Castle.Core(自动接管)

模块化宿主同时引用 Galosys.Foundation.Castle.Core + Galosys.Foundation.Polly.Core 时:

  • PollyCoreModule 自动注册默认 retry 命名管道 + AddPollyResilience()
  • 用户在业务方法上添加 [Retryable] 即自动启用真实重试,无需手动调用
[Retryable]
public virtual async Task<string> CallApiAsync()
{
    // 自动注入 retry 命名管道(MaxRetries = 3, Exponential)
}

RabbitMQ.Client(发布重试)

services.AddResilienceCore("rabbitmq-publish", builder => { /* retry / circuit breaker */ });
services.AddPollyResilience();

测试

Galosys.Foundation.Polly.Core.Tests:20 个用例(策略 + PipelineBuilder + RetryableAttributeTest),覆盖 Retry / CircuitBreaker / Bulkhead / Timeout / Fallback / 命名管道 / [Retryable] 拦截器行为。

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on Galosys.Foundation.Polly.Core:

Package Downloads
Galosys.Foundation.RabbitMQ.Client

Galosys.Foundation快速开发库

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.9.16.1 39 9/16/2026
26.9.15.1 48 9/15/2026
26.9.14.1 45 9/15/2026