Galosys.Foundation.Castle 26.5.20.1

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

Galosys.Foundation.Castle

成熟度: 🟢 稳定 — 生产可用,测试充分,活跃维护

简介

基于 Castle.DynamicProxy 的 AOP 拦截模块,提供动态代理、特性驱动的拦截器,支持事务管理、重试策略、幂等性控制和异常日志记录。

特性

  • 基于特性的声明式 AOP 拦截
  • 支持异步方法拦截(Task/ValueTask)
  • 内置事务管理拦截器 [Transactional]
  • 内置重试策略拦截器 [Retryable](基于 Polly)
  • 内置幂等性控制拦截器 [Idempotent]
  • 内置异常日志拦截器 [CatchLogging]
  • 自动扫描注册拦截器
  • 支持接口和类的动态代理
  • 支持 DI 容器集成

安装

dotnet add package Galosys.Foundation.Castle

使用

注册服务

// Program.cs - 方式一:使用 CastleCoreServiceProviderFactory
builder.Host.UseCastleCoreServiceProvider();

// Program.cs - 方式二:手动注册(需在所有服务注册后调用)
builder.Services.AddCastleInterceptors();
builder.Services.ConfigureCastleDynamicProxy();

事务管理

public class OrderService
{
    [Transactional]
    public virtual async Task CreateOrderAsync(Order order)
    {
        // 方法执行在事务中
    }

    [Transactional(IsolationLevel = IsolationLevel.Serializable, Timeout = 30000)]
    public virtual void ProcessPayment(Payment payment)
    {
        // 自定义隔离级别和超时时间
    }
}

重试策略

public class ExternalApiService
{
    [Retryable(maxAttempts: 3)]
    public virtual async Task<string> CallApiAsync()
    {
        // 失败时自动重试 3 次
    }

    [Retryable(maxAttempts: 5, MaxDelay = 10000, Multiplier = 2)]
    public virtual async Task<Data> FetchDataAsync()
    {
        // 指数退避重试,最大延迟 10 秒
    }

    [Retryable(RetryFor = typeof(HttpRequestException))]
    public virtual async Task<Response> ResilientCallAsync()
    {
        // 仅对特定异常类型重试
    }
}

幂等性控制

public class PaymentService
{
    [Idempotent("payment:process")]
    public virtual async Task<PaymentResult> ProcessPaymentAsync(PaymentRequest request)
    {
        // 相同 Key 的重复调用将返回缓存结果
    }
}

异常日志记录

public class BusinessService
{
    [CatchLogging]
    public virtual async Task ExecuteAsync()
    {
        // 执行耗时和异常信息将自动记录
    }
}

自定义拦截器

// 方式一:继承 InterceptorAttribute
public class CustomInterceptorAttribute : InterceptorAttribute
{
    protected override async ValueTask InterceptAsync(IAsyncInvocation invocation)
    {
        // 前置逻辑
        await invocation.ProceedAsync();
        // 后置逻辑
    }
}

// 方式二:继承 InterceptorBase
public class CustomInterceptor : InterceptorBase
{
    protected override async ValueTask InterceptAsync(IAsyncInvocation invocation)
    {
        await invocation.ProceedAsync();
    }
}

// 使用
public class MyService
{
    [CustomInterceptor]
    public virtual void DoSomething() { }
}

核心类

说明
InterceptorAttribute 拦截器特性基类,支持异步方法
InterceptorBase 传统拦截器基类
TransactionalAttribute 事务管理拦截器
RetryableAttribute 重试策略拦截器(Polly)
IdempotentAttribute 幂等性控制拦截器
CatchLoggingAttribute 异常日志记录拦截器
CastleCoreServiceCollectionExtensions DI 容器扩展方法
CastleCoreHostBuilderExtensions Host 构建器扩展方法
IAsyncInvocation 异步调用上下文接口

注意事项

  • 拦截器内部不应再嵌套其他拦截器,避免递归调用
  • 日志拦截器上不应再添加拦截器
  • ConfigureCastleDynamicProxy 必须在所有服务注册完成后调用
  • 使用特性的类必须声明为 virtual 方法或实现接口

依赖

  • Castle.Core
  • Polly.Core
  • Galosys.Foundation.Core
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.

Version Downloads Last Updated
26.5.20.1 90 5/20/2026
26.5.19.1 93 5/19/2026
26.5.18.1 90 5/18/2026
26.5.15.1 85 5/15/2026
26.5.12.3 90 5/12/2026
26.5.12.2 98 5/12/2026
26.4.27.1-rc1 99 4/26/2026
26.4.25.1-rc1 91 4/25/2026
26.4.22.2-rc7 100 4/22/2026
26.4.22.2-rc6 89 4/22/2026
26.4.22.2-rc4 90 4/22/2026
26.4.22.2-rc3 90 4/22/2026
26.4.19.1-rc1 92 4/19/2026
26.4.12.8-rc1 94 4/12/2026
26.4.12.7-rc1 92 4/12/2026
26.1.30.1-rc1 115 1/30/2026
26.1.29.1 118 1/29/2026
26.1.28.5 113 1/28/2026
26.1.28.4 113 1/28/2026
26.1.28.2 110 1/28/2026
Loading failed