添加项目
、1.打开VS2005 ,新建一个名为ExceptionStudy的解决方数,并添加一个名为QuickStartExcutable的Window应用程序项目。
2.添加一个名为BusinessLayerDLL类库项目。
添加引用
3.为解决方案添加以下相关的Enterprise Library DLL(放在安装目录下的bin 文件夹中),把它们复制到QuickStartExcutable项目的bin\Debug目录下。
. Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
. Microsoft.Practices.ObjectBuilder.dll
. Microsoft.Practices.EnterpriseLibrary.Common.dll
4.在QuickStartExcutable项目中添加引用,浏览到上面三个DLL,项目里添加BusinessLayerDLL.DLL,.NET里添加System.Configuration 一共五个引用;
在BusinessLayerDLL项目里添加
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
Microsoft.Practices.EnterpriseLibrary.Common.dll
的引用。
添加异常处理
5.在BusinessLayerDLL类库项目中添加一个Servies类新文件,并在其中添加两个办法来实现引发异常与处理异常情况。
6.在QuickStartExcutable项目中添加一个AppMessageExceptionHandler的新文件,来实现自定义的异常处理,其要必须符合两个条件:
A.实现继承IExceptionHandler接品口
B.类声明 [ConfigurationElementType(typeof(CustomHandlerData))]属性
7.在Form1里添加一个Button名为b_CatchException,添加Button的事件处理的办法如下:
创建异常处理策略配置文件:
8.打开Enterprise Library Configuration ,右键New Applicaton,设置ConfigurationFile,把它的路径选择在ExceptionStudy存放的目录下,并命名为App
9.右键->New->Exception Handling Application Bock
10.右键->New->Exception Policy,Name为Propagate Policy
8.右键->New->Exception Type ,类型为Exception,PostHandlingAction为NotifyRethrow.
11.重复7,Name为:Global Policy,
12,重复8, PostHandlingAction为None;
13.右键->New->Custom Handler ,设置type ->Load Assembly->浏览到 QuickStartExcutable项目的文件夹,在bin\Debug下选择QuickStartExcutable.exe,点击OK即可。
14.分别在两个项目里,添加现有项,文件类型为所有文件类型,然后找到上面创建的配置文件,添加就行了。
15.到这里整个解决方案就完成了,运行程序就可以啦。点击Button后就可以显示异常的原始信息。
参考文档ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.EntLib.2006Jan/EnterpriseLibrary/html/