在WCF中实现全局错误捕获

 

在 Web Applications中我们可以在Global.asax中通过Application_Error捕获应用程序错误。
在ASMX Web Services中我们可以写一个Soap Extension在程序异常被发送到客户端之前将其捕获并进行处理。
如果想在WCF中实现以下功能,当Server端程序出现异常时,程序可以捕获所有异常并进行写日志、通知管理员等处理。我们可以为每个Server端的方法加入try....catch...finally块,但这样写太麻烦。
实际上,在WCF中我们可以通过以下方式实现全局错误捕获:
1  MSDN中讲到,在System.ServiceModel.Dispatcher命名空间下有个IErrorHandler 接口。允许实施者对返回给调用方的错误消息进行控制,还可以选择执行自定义错误处理,例如日志记录。参见 http://msdn2.microsoft.com/zh-cn/library/system.servicemodel.dispatcher.ierrorhandler.aspx 
2 实现方法示例:(以下示例仅仅按最简单的方式去实现WCF全局错误捕获)
   定义一个类包含静态事件用于发生错误时触发该事件,代码如下:

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.ComponentModel;

namespace  BNCommon.ServiceHelper
{
    
public class BNServiceEvents
    
{
        
protected static EventHandlerList listEventDelegates = new EventHandlerList();

        
static readonly object HandleServiceMethodExecErrorKey = new object();
        
public delegate void HandleServiceMethodExecError(Exception ex);
        
public static event HandleServiceMethodExecError EventServiceMethodExecError
        
{
            add 
{ listEventDelegates.AddHandler(HandleServiceMethodExecErrorKey, value); }
            remove 
{ listEventDelegates.RemoveHandler(HandleServiceMethodExecErrorKey, value); }
        }

        
public static void FireEventServiceMethodExecError(Exception ex)
        
{
            HandleServiceMethodExecError handler 
= (HandleServiceMethodExecError)listEventDelegates[HandleServiceMethodExecErrorKey];
            
if (handler != null)
            
{
                handler(ex);
            }

        }
     
    
        
       
    }

}

增加一个类实现System.ServiceModel.Dispatcher.IErrorHandler 接口,代码如下:

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.ServiceModel;
using  System.ServiceModel.Dispatcher;
using  System.ServiceModel.Description;
using  System.ServiceModel.Channels;
using  System.ServiceModel.Configuration;
namespace  BNCommon.ServiceHelper

    
public class BNErrorHandler : System.ServiceModel.Dispatcher.IErrorHandler
    
{
        
IErrorHandler 成员
    }

}

增加一个类实现System.ServiceModel.Description.IServiceBehavior接口并继承System.ServiceModel.Configuration.BehaviorExtensionElement用于将错误捕获行为加入Service行为集合中,代码如下:

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.ServiceModel;
using  System.ServiceModel.Dispatcher;
using  System.ServiceModel.Description;
using  System.ServiceModel.Channels;
using  System.ServiceModel.Configuration;
namespace  BNCommon.ServiceHelper
{
    
public class BNServiceOperationBehavior : BehaviorExtensionElement, IServiceBehavior
    
{
       
BehaviorExtensionElement成员


       
IServiceBehavior 成员
    }

}

在实例化ServiceHost时将扩展的Service行为加入行为集合中(也可以通过配置文件的方式实现,这里使用代码实现):

ServiceHost sh  =   new  ServiceHost(types[i]);
sh.Description.Behaviors.Add(
new  BNServiceOperationBehavior());

在宿主程序中订阅BNServiceEvents.EventServiceMethodExecError事件进行处理,代码如下:

using  System;
using  System.Collections.Generic;
using  System.Text;
using  BNCommon.ServiceHelper;

namespace  BNClinicService.ServiceConsole
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            System.Console.WriteLine(
"Press <ENTER> to start service.");
            System.Console.ReadLine();

            
//订阅异常事件
            BNCommon.ServiceHelper.BNServiceEvents.EventServiceMethodExecError += new BNServiceEvents.HandleServiceMethodExecError(BNServiceEvents_EventServiceMethodExecError);
            
//启动服务
            BNIIServiceLayer.SecurityServiceHosting.StartService();

            
            System.Console.WriteLine(
"Press <ENTER> to stop service.");
            System.Console.ReadLine();
            
//停止服务            
            BNIIServiceLayer.SecurityServiceHosting.StopService();
            
        }
             

        
static void BNServiceEvents_EventServiceMethodExecError(Exception ex)
        
{
            
//写日志....
            BNIVSericeLayer.BNServiceLogEvent.FireLogEvent(BNIVSericeLayer.LogHelper.GetFaultLogModel(ex.Source, string.Empty, ex.Message, string.Empty));
            
//其他处理....
        }

    }

}

 



 




 
  

 

 

  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值