旋风

Communication comes from our hears and heads! my msn:youpeizun@hotmail.com


导航

EnterpriseLibrary2(异常的传播与处理)

Posted on 2006-08-28 12:59  xuanfeng  阅读(913)  评论(0编辑  收藏  举报

 描述:这个程序演示了异常在两个项目中的传播。

项目的静态结构图:


 

   添加项目

1.打开VS2005 ,新建一个名为ExceptionStudy的解决方数,并添加一个名为QuickStartExcutableWindow应用程序项目。

 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类新文件,并在其中添加两个办法来实现引发异常与处理异常情况。

 

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace BusinessLayerBLL
{
      
public  class Servies

    
{
          
public Servies()
          

          
          }

           
private  void ProcessA()
        
{
            
//在这个线程里发生异常
            throw new System.Exception("Original Exception: Fatal exception in business layer");
        }

          
public bool ProcessWithPropagate()
          
{
              
try
              
{ // 引发异常;
                  this.ProcessA();
              }

              
catch (Exception ex)
              
{
                  
//Propagate Policy异常策略声明会再一次抛出异常在此不做处理,返回True值
                  bool rethrow = ExceptionPolicy.HandleException(ex, "Propagate Policy");

                  
if (rethrow)
                  
{
                      
throw;
                  }

              }


              
return true;
          }

    }

}

 

6.QuickStartExcutable项目中添加一个AppMessageExceptionHandler的新文件,来实现自定义的异常处理,其要必须符合两个条件:

 A.实现继承IExceptionHandler接品口

 B.类声明 [ConfigurationElementType(typeof(CustomHandlerData))]属性

using System.IO;
using System;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using System.Collections.Specialized;

namespace QuickStactExcutable
{
    [ConfigurationElementType(
typeof(CustomHandlerData))]
    
public class AppMessageExceptionHandler : IExceptionHandler
    
{
        
public AppMessageExceptionHandler(NameValueCollection ignore)
        
{
        }

        
//实现IExeptionHandler唯一的一个成员
        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        
{
            
//对异常进行处理
            DialogResult result = this.ShowThreadExceptionDialog(exception);

            
return exception;
        }


        
//自定义处理异常的办法,将异常显示出来
        private DialogResult ShowThreadExceptionDialog(Exception ex)
        
{
            
string errorMsg = "The following exception was caught by the Quick Start Global Exception Handler:" + Environment.NewLine + Environment.NewLine;

            StringBuilder sb 
= new StringBuilder();
            StringWriter writer 
= new StringWriter(sb);

            TextExceptionFormatter formatter 
= new TextExceptionFormatter(writer,ex);

            
// Format the exception
            formatter.Format();

            errorMsg 
+= sb.ToString();

            
return MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }


    }

}




7.Form1里添加一个Button名为b_CatchException,添加Button的事件处理的办法如下:

b_CatchException_Click

 

   创建异常处理策略配置文件:

8.打开Enterprise Library Configuration ,右键New Applicaton,设置ConfigurationFile,把它的路径选择在ExceptionStudy存放的目录下,并命名为App

9.右键->New->Exception Handling Application Bock

10.右键->New->Exception Policy,NamePropagate Policy

8.右键->New->Exception Type ,类型为Exception,PostHandlingActionNotifyRethrow.

11.重复7,Name为:Global Policy,

12,重复8, PostHandlingActionNone;

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/04-06-020-Walkthrough%20%20Propagating%20the%20Original%20Exception.htm