Castle.DynamicProxy拦截器

Posted dotNET跨平台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Castle.DynamicProxy拦截器相关的知识,希望对你有一定的参考价值。

在asp.net mvc或asp.net miniapi中,有过滤器,可以在请求前或后增加一层,达到验证,过滤等作用,如果在Service的方法前后加一层呢?这里介绍一下Castle.DynamicProxy的用法。

首先引入

Castle.Core

实现代码相对轻量:

using Castle.DynamicProxy;
using Microsoft.Extensions.DependencyInjection.Extensions;


var builder = WebApplication.CreateBuilder(args);
builder.Services.AddInterceptedSingleton<ITestService, TestService, AddLogInterceptor>();


var app = builder.Build();


app.MapGet("/test", (ITestService test) =>

    app.Logger.LogInformation("/test开始");
    var result = test.Get(121);
    app.Logger.LogInformation("/test结束");
    return result;
);


app.Run();


public interface ITestService

    string Get(int id);

public class TestService : ITestService

    private readonly ILogger<TestService> _logger;
    public TestService(ILogger<TestService> logger)
    
        _logger = logger;
    
    public string Get(int id)
    
        _logger.LogInformation("TestService.Get(id)", id);
        return "OK";
    

public class AddLogInterceptor : IInterceptor

    private readonly ILogger<AddLogInterceptor> _logger;
    public AddLogInterceptor(ILogger<AddLogInterceptor> logger)
    
        _logger = logger;
    
    public void Intercept(IInvocation invocation)
    
        //用invocation可以获取被调用对象和方法的信息
        _logger.LogInformation("开始调用name,参数:args", invocation.Method.Name, string.Join(",", invocation.Arguments));
        invocation.Proceed();
        _logger.LogInformation("结束调用name,返回结果:result", invocation.Method.Name, invocation.ReturnValue);
    

public static class InterceptedExpansion

    public static void AddInterceptedSingleton<TIService, TService, TInterceptor>(this IServiceCollection services)
        where TIService : class
        where TService : class, TIService
        where TInterceptor : class, IInterceptor
    
        services.TryAddSingleton<IProxyGenerator, ProxyGenerator>();
        services.AddSingleton<TService>();
        services.TryAddTransient<TInterceptor>();
        services.AddSingleton(provider =>
        
            var proxyGenerator = provider.GetRequiredService<IProxyGenerator>();
            var service = provider.GetRequiredService<TService>();
            var interceptor = provider.GetRequiredService<TInterceptor>();
            return proxyGenerator.CreateInterfaceProxyWithTarget<TIService>(service, interceptor);
        );
    

效果如下:

以上是关于Castle.DynamicProxy拦截器的主要内容,如果未能解决你的问题,请参考以下文章

Autofac + Castle DynamicProxy:拦截器的顺序

.NET Core 默认依赖注入与 Castle DynamicProxy

如何从 Castle.DynamicProxy 中的 ProxyGenerationHook 访问自定义方法属性

如何在 Castle.DynamicProxy 中使用 IInterceptor?

Castle DynamicProxy

在 C# 中使用 Castle.DynamicProxy 对数组进行排序