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); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 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)解读