博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我心中的核心组件(可插拔的AOP)~第四回 异常拦截器
阅读量:6963 次
发布时间:2019-06-27

本文共 3235 字,大约阅读时间需要 10 分钟。

之前说过有关拦截器的文章,,事实上,在那讲里说的最多是AOP和缓存组件,对于拦截的概念并没有详细的说明,这一讲,不说AOP,主要说一下拦截器,拦截器Interception,主要是在方法执行前或者执行后,动态添加一些行为,而这个行为主要包含缓存,日志,异常处理及你可以想到的所有的一切,呵呵。

这一讲是异常拦截器,它的主要意义在于,当你的一个方法被执行时,你可以通过配置文件去管理这个方法执行前与执行后是否附加统一异常处理行为。

拦截器组件我们还是用Unity.InterceptionExtension,它依附于Unity,当你没有安装Unity时,Unity.InterceptionExtension在安装时会自己帮你添加上,这是正确的,呵呵。

对于我们所开发的拦截器,必须要实现IInterceptionBehavior这个接口才可以,这是接口的内容

// 摘要:    //     Interception behaviors implement this interface and are called for each invocation    //     of the pipelines that they're included in.    public interface IInterceptionBehavior    {        // 摘要:        //     Returns a flag indicating if this behavior will actually do anything when        //     invoked.        //        // 备注:        //     This is used to optimize interception. If the behaviors won't actually do        //     anything (for example, PIAB where no policies match) then the interception        //     mechanism can be skipped completely.        bool WillExecute { get; }        // 摘要:        //     Returns the interfaces required by the behavior for the objects it intercepts.        //        // 返回结果:        //     The required interfaces.        IEnumerable
GetRequiredInterfaces(); // // 摘要: // Implement this method to execute your behavior processing. // // 参数: // input: // Inputs to the current call to the target. // // getNext: // Delegate to execute to get the next delegate in the behavior chain. // // 返回结果: // Return value from the target. IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext); }
View Code

其中Invoke方法就是拦截方法之前要执行的方法,当进行目标方法时,首先会检测是否在此方法的信息,如果有,就进行拦截行为,本例为异常拦截行为。

异常拦截器代码:

///     /// 表示用于异常日志记录的拦截行为。    ///     public class ExceptionLoggingBehavior : IInterceptionBehavior    {        #region IInterceptionBehavior Members        ///         /// 获取当前行为需要拦截的对象类型接口。        ///         /// 
所有需要拦截的对象类型接口。
public IEnumerable
GetRequiredInterfaces() { return Type.EmptyTypes; } ///
/// 通过实现此方法来拦截调用并执行所需的拦截行为。 /// ///
调用拦截目标时的输入信息。 ///
通过行为链来获取下一个拦截行为的委托。 ///
从拦截目标获得的返回信息。
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { var methodReturn = getNext().Invoke(input, getNext); if (methodReturn.Exception != null) { Utils.Log(methodReturn.Exception); } return methodReturn; } ///
/// 获取一个
值,该值表示当前拦截行为被调用时,是否真的需要执行 /// 某些操作。 ///
public bool WillExecute { get { return true; } } #endregion }

而要想使拦截器起作用,我们可以在配置文件中进行配置,下面是缓存拦截与异常拦截的配置代码,它们隶属于Unity节点

怎么样,大家是否对拦截器有一个很清楚的了解和认识了呢,呵呵!

 

转载于:https://www.cnblogs.com/lori/p/3250797.html

你可能感兴趣的文章
实验二
查看>>
《世界因你不同》-笔记
查看>>
洛谷P1966 火柴排队[NOIP提高组2013]
查看>>
验证SQLServer死锁进程
查看>>
MySQL中kill掉所有表的进程
查看>>
毛玻璃效果
查看>>
ETW (Event Tracing for Windows)介绍
查看>>
另一种阶乘问题
查看>>
三十三、MySQL 导入数据
查看>>
侧耳倾听
查看>>
Android中的Shape使用总结
查看>>
数据结构与算法面试题80道(33)
查看>>
[vijos P1626] 爱在心中
查看>>
jQuery 缺点
查看>>
洛谷 P1598 垂直柱状图【字符串】
查看>>
L1-1. 出生年【STL放的位置】
查看>>
dockerfile
查看>>
ELF学习--可执行文件
查看>>
linux入门
查看>>
centos7 mysql-server 安装过程
查看>>