遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

MVC中使用Unity AOP

Unity AOP支持,透明代码,接口方式,与Virtual 方法, 由于透明代理必须继承MarshalByRefObject,接口方式使用Unity构建时,只能按接口获取

Container.Resolve(IXXX),  在让标记接口继承IController(Filter正常)后发现Unity无法拦截到,最后只有Virtal方法方式可以形, Virtual方式的拦截器会在Filter执行后执行,Virtaul方法编织进AOP代码后取代了原来的Controll方法

 

基于虚方法方式,Unity生成的代理类,该方式属于类型拦截,不存在拦截(代理)实例到目标实例(target)的引用

public class Wrapped_Home_273249ec14ea46568e66523bd7e6877c : Home, IInterceptingProxy
{
    // Fields
    private readonly InterceptionBehaviorPipeline pipeline = new InterceptionBehaviorPipeline();
 
    // Methods
    [CompilerGenerated]
    private IMethodReturn <Index_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
    {
        try
        {
            int returnValue = base.Index();
            return inputs.CreateMethodReturn(returnValue, new object[0]);
        }
        catch (Exception exception)
        {
            return inputs.CreateExceptionMethodReturn(exception);
        }
    }
 
    public override int Index()
    {
        VirtualMethodInvocation input = new VirtualMethodInvocation(this, methodof(Home.Index), new object[0]);
        IMethodReturn return2 = this.pipeline.Invoke(input, new InvokeInterceptionBehaviorDelegate(this.<Index_DelegateImplementation>__0));
        Exception exception = return2.Exception;
        if (exception != null) throw exception;
        return (int) return2.ReturnValue;
    }
 
    void IInterceptingProxy.AddInterceptionBehavior(IInterceptionBehavior interceptor)
    {
        this.pipeline.Add(interceptor);
    }
}

 

 

基于接口方式,Unity生成的代理类--注意接口方式(InterfaceInterceptor)跟透明代理(TransparentProxyInterceptor)一样都是实例拦截器

public class Wrapped_IHome_ff5a91d8f8b1447c924a45e62dcb89e8 : IInterceptingProxy, IHome
{
    // Fields
    private readonly InterceptionBehaviorPipeline pipeline = new InterceptionBehaviorPipeline();
    private IHome target;
    private Type typeToProxy;
 
    // Methods
    public Wrapped_IHome_ff5a91d8f8b1447c924a45e62dcb89e8(IHome target, Type typeToProxy)
    {
        this.target = target;
        this.typeToProxy = typeToProxy;
    }
 
    [CompilerGenerated]
    private IMethodReturn <Index_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
    {
        try
        {
            int returnValue = this.target.Index();
            return inputs.CreateMethodReturn(returnValue, new object[0]);
        }
        catch (Exception exception)
        {
            return inputs.CreateExceptionMethodReturn(exception);
        }
    }
 
    public int Index()
    {
        VirtualMethodInvocation input = new VirtualMethodInvocation(this.target, methodof(IHome.Index), new object[0]);
        IMethodReturn return2 = this.pipeline.Invoke(input, new InvokeInterceptionBehaviorDelegate(this.<Index_DelegateImplementation>__0));
        Exception exception = return2.Exception;
        if (exception != null) throw exception;
        return (int) return2.ReturnValue;
    }
 
    void IInterceptingProxy.AddInterceptionBehavior(IInterceptionBehavior interceptor)
    {
        this.pipeline.Add(interceptor);
    }
}

基于接口的动态生成类

 

public interface IAOP:IController
  {
      ActionResult Index();
  }

 下面代码中pipeline中只有一个元素那就是PolicyInjectionBehavior,而PolicyInjectionBehavior里面又根据调用的方法来维护一组ICallHandler管道

public class Wrapped_IHome_fa19b49db3a345b28531be422717e4e7 : IInterceptingProxy, IHome, IController
{
    // Fields
    public readonly InterceptionBehaviorPipeline pipeline = new InterceptionBehaviorPipeline();
    private IHome target;
    private Type typeToProxy;
 
    // Methods
    public Wrapped_IHome_fa19b49db3a345b28531be422717e4e7(IHome target, Type typeToProxy)
    {
        this.target = target;
        this.typeToProxy = typeToProxy;
    }
 
    [CompilerGenerated]
    private IMethodReturn <Execute_DelegateImplementation>__1(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
    {
        try
        {
            this.target.Execute();
            return inputs.CreateMethodReturn(null, new object[0]);
        }
        catch (Exception exception)
        {
            return inputs.CreateExceptionMethodReturn(exception);
        }
    }
 
    [CompilerGenerated]
    private IMethodReturn <Index_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
    {
        try
        {
            int returnValue = this.target.Index();
            return inputs.CreateMethodReturn(returnValue, new object[0]);
        }
        catch (Exception exception)
        {
            return inputs.CreateExceptionMethodReturn(exception);
        }
    }
 
    public void Execute()
    {
        VirtualMethodInvocation input = new VirtualMethodInvocation(this.target, methodof(IController.Execute), new object[0]);
        Exception exception = this.pipeline.Invoke(input, new InvokeInterceptionBehaviorDelegate(this.<Execute_DelegateImplementation>__1)).Exception;
        if (exception != null) throw exception;
    }
 
    public int Index()
    {
        VirtualMethodInvocation input = new VirtualMethodInvocation(this.target, methodof(IHome.Index), new object[0]);
        IMethodReturn return2 = this.pipeline.Invoke(input, new InvokeInterceptionBehaviorDelegate(this.<Index_DelegateImplementation>__0));
        Exception exception = return2.Exception;
        if (exception != null) throw exception;
        return (int) return2.ReturnValue;
    }
 
    void IInterceptingProxy.AddInterceptionBehavior(IInterceptionBehavior interceptor)
    {
        this.pipeline.Add(interceptor);
    }
}

 

posted on   遗忘海岸  阅读(1042)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2008-02-22 siteFactory 防刷新模块RefreshModule
2008-02-22 动易SiteFactory配置文件(web.config)解读
点击右上角即可分享
微信分享提示