回到目录

之前说过有关拦截器的文章,第二回  缓存拦截器,事实上,在那讲里说的最多是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<Type> 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);
}

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

异常拦截器代码:

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

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

  <!--BEGIN: Unity-->
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
<container>
<extension type="Interception" />
<register type="Infrastructure.Caching.ICacheProvider, DDD_AOP_WCF.Infrastructure" mapTo="Infrastructure.Caching.EntLibCacheProvider, DDD_AOP_WCF.Infrastructure" />
<!--对数据上下文的注入-->
<register type="DDD_AOP_WCF.Domain.Repository.IProductRepository, DDD_AOP_WCF.Domain" mapTo="DDD_AOP_WCF.Infrastructure.Repository.ProductRepository, DDD_AOP_WCF.Infrastructure" />
<!--对WCF的访问进行的注入与缓存和异常的拦截-->
<register type="DDD_AOP_WCF.ServiceContracts.IProductService, DDD_AOP_WCF.ServiceContracts" mapTo="DDD_AOP_WCF.Service.Implements.ProductServiceImpl, DDD_AOP_WCF.Service">
<!-- <interceptor type="VirtualMethodInterceptor" />-->
<interceptor type="InterfaceInterceptor" />
<interceptionBehavior type="Infrastructure.InterceptionBehaviors.CachingBehavior, DDD_AOP_WCF.Infrastructure" />
<interceptionBehavior type="Infrastructure.InterceptionBehaviors.ExceptionLoggingBehavior, DDD_AOP_WCF.Infrastructure" />
</register>
</container>
</unity>
<!--END: Unity-->

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

回到目录

我心中的核心组件(可插拔的AOP)~第四回 异常拦截器的更多相关文章

  1. 我心中的核心组件(可插拔的AOP)~第二回 缓存拦截器

    回到目录 AOP面向切面的编程,也称面向方面的编程,我更青睐于前面的叫法,将一个大系统切成多个独立的部分,而这个独立的部分又可以方便的插拔在其它领域的系统之中,这种编程的方式我们叫它面向切面,而这些独 ...

  2. 我心中的核心组件(可插拔的AOP)~大话开篇及目录

    回到占占推荐博客索引 核心组件 我心中的核心组件,核心组件就是我认为在项目中比较常用的功能,如日志,异常处理,消息,邮件,队列服务,调度,缓存,持久化,分布式文件存储,NoSQL存储,IoC容器,方法 ...

  3. 我心中的核心组件(可插拔的AOP)~第五回 消息组件

    回到目录 之所以把发消息拿出来,完全是因为微软的orchard项目,在这个项目里,将公用的与领域无关的功能模块进行抽象,形成了一个个的组件,这些组件通过引用和注入的方式进行工作,感觉对于应用程序的扩展 ...

  4. 我心中的核心组件(可插拔的AOP)~第六回 消息组件~续

    回到目录 上一回写消息组件已经是很久之前的事了,这一次准备把消息组件后续的东西说一下,事实上,第一篇文章主要讲的是发消息,而这一讲最要讲的是收消息,简单的说,就是消息到了服务器之后,如何从服务器实时的 ...

  5. 我心中的核心组件(可插拔的AOP)~调度组件quartz.net

    回到目录 quartz.net是一个任务调度组件,它可以灵活的设置你的调试方式,按时间,按日期,按周期都可以很容易的实现,quartz不仅可以用在web中,而且还可以部署在winform,winser ...

  6. 我心中的核心组件(可插拔的AOP)~第十二回 IoC组件Unity

    回到目录 说在前 Ioc组件有很多,之前也介绍过autofac,castle等,今天再来说一下在微软Nlayer DDD架构里使用的unity组件,今天主要说一下依靠注入,如果希望看拦截的用法,可以阅 ...

  7. 我心中的核心组件(可插拔的AOP)~第十三回 实现AOP的拦截组件Unity.Interception

    回到目录 说在前 本节主要说一下Unity家族里的拦截组件,对于方法拦截有很多组件提供,基本上每个Ioc组件都有对它的实现,如autofac,它主要用在orchard项目里,而castle也有以拦截的 ...

  8. 我心中的核心组件(可插拔的AOP)~第十五回 我的日志组件Logger.Core(策略,模版方法,工厂,单例等模式的使用)

    回到目录 之前的讲过两篇关于日志组件的文章,分别是<第一回  日志记录组件之自主的Vlog>和<第三回  日志记录组件之log4net>,而今天主要说一下我自己开发的另一种日志 ...

  9. 我心中的核心组件(可插拔的AOP)~调度组件quartz.net续~任务管理器的开发

    回到目录 对于任务调度来说,越来越多的团队选择了quartz,它在java和.net环境下表现都十分优秀,配置简单,功能强大,时间表达式配置灵活,但在使用时,还是感觉缺点什么,怎么说,你在服务器上安装 ...

随机推荐

  1. ros机器人开发概述

    1.       ROS项目开发流程? 参照古月大神写的ROS探索总结系列:http://blog.exbot.net/archives/619 具体项目设计可看看<程序员>杂志的最新一篇 ...

  2. js 正则表达式

    //判断字符串是否为数字 function checkRate(input) { var re = /^[0-9]+.?[0-9]*$/; if (!re.test(input.rate.value) ...

  3. 【分块打表】bzoj3758 数数

    验证一个数是不是优美的:设数位之和为sum,若sum mod 2 != 0,则不优美.否则考虑枚举这个数的每一位,将之前所有位任意相加产生的所有 数字和 和 当前位 的和塞到集合里,最终判断集合中是否 ...

  4. Webservice 65535 错误

    修改配置项: <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name=&qu ...

  5. Android应用:StatusBar状态栏、NavigationBar虚拟按键栏、ActionBar标题栏、Window屏幕内容区域等的宽高

    一.屏幕中各种栏目以及屏幕的尺寸 当我们需要计算屏幕中一些元素的高度时,或许需要先获取到屏幕或者各种栏目的高度,下面这个类包含了Status bar状态栏,Navigation bar虚拟按键栏,Ac ...

  6. being词典案例分析

    一.调研评测: 1.软件bug: 1.输入空格分号回车之后并不给用户报错,说明他的异常处理机制有问题. 2.对于中文的很多口头语和方言,并不能给出翻译或者说,也并没有给出网络搜索后的结果. 3.添加生 ...

  7. css+js回到顶部

    .backToTop { display: none; width: 18px; line-height: 1.2; padding: 5px 0; background-color: #000; c ...

  8. iOS之NSString类中compare方法的陷阱

    typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDe ...

  9. JS中innerHTML,innerText,value

    一·.JS初学者易混淆的问题:innerHTML,innerText,value(他们和JQ的区别:JS→value,JQ→value()) 1.getElementById("a" ...

  10. ABP框架详解(四)Feature

    ABP框架中存在一个Feature的特性,功能和设计思路非常类似于框架中的Authorization功能,都是来控制用户是否能够继续操作某项功能,不同点在于Authorization默认是应用在IAp ...