WCF全局错误捕获正确内容解析

开发 开发工具
WCF全局错误捕获在我们的实际应用中将会给大家带来许多帮助。在这里我们将会针对这一应用技巧给大家带来一些帮助。

WCF开发插件的利用,为我们的程序开发实现了许多新的功能。而且在处理错误异常的时候,表现尤为突出。在这里我们将会为大家详细介绍一下有关WCF全局错误捕获的相关内容,希望对大家有所帮助。#t#

在 Web Applications中我们可以在Global.asax中通过Application_Error捕获应用程序错误。在ASMX Web Services中我们可以写一个Soap Extension在程序异常被发送到客户端之前将其捕获并进行处理。

如果想在WCF中实现以下功能,当Server端程序出现异常时,程序可以捕获所有异常并进行写日志、通知管理员等处理。我们可以为每个Server端的方法加入try....catch...finally块,但这样写太麻烦。

实际上,在WCF中我们可以通过以下方式实现WCF全局错误捕获:

1 MSDN中讲到,在System.ServiceModel.Dispatcher命名空间下有个IErrorHandler 接口。允许实施者对返回给调用方的错误消息进行控制,还可以选择执行自定义错误处理,例如日志记录。

2 实现方法示例:(以下示例仅仅按最简单的方式去实现WCF全局错误捕获)

定义一个类包含静态事件用于发生错误时触发该事件,代码如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.ComponentModel;  
  5. namespace BNCommon.ServiceHelper  
  6. ...{  
  7. public class BNServiceEvents  
  8. ...{  
  9. protected static EventHandlerList listEventDelegates = 
    new EventHandlerList();  
  10. static readonly object HandleServiceMethodExecErrorKey = 
    new object();  
  11. public delegate void HandleServiceMethodExecError(Exception ex);  
  12. public static event HandleServiceMethodExecError 
    EventServiceMethodExecError  
  13. ...{  
  14. add ...{ listEventDelegates.AddHandler(HandleServiceMethod
    ExecErrorKey, value); }  
  15. remove ...{ listEventDelegates.RemoveHandler(HandleServiceMethod
    ExecErrorKey, value); }  
  16. }  
  17. public static void FireEventServiceMethodExecError(Exception ex)  
  18. ...{  
  19. HandleServiceMethodExecError handler = (HandleServiceMethodExecError)
    listEventDelegates[HandleServiceMethodExecErrorKey];  
  20. if (handler != null)  
  21. ...{  
  22. handler(ex);  
  23. }  
  24. }   
  25. }  

 

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

 

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.ServiceModel;  
  5. using System.ServiceModel.Dispatcher;  
  6. using System.ServiceModel.Description;  
  7. using System.ServiceModel.Channels;  
  8. using System.ServiceModel.Configuration;  
  9. namespace BNCommon.ServiceHelper  
  10. ...{   
  11. public class BNErrorHandler : System.ServiceModel.
    Dispatcher.IErrorHandler  
  12. ...{  
  13. IErrorHandler 成员#region IErrorHandler 成员  
  14. public bool HandleError(Exception error)  
  15. ...{  
  16. //异常发生时触发事件  
  17. BNServiceEvents.FireEventServiceMethodExecError(error);  
  18. return true;  
  19. }  
  20. public void ProvideFault(Exception error, MessageVersion
     version, ref Message fault)  
  21. ...{  
  22. }  
  23. #endregion  
  24. }  

 

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

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.ServiceModel;  
  5. using System.ServiceModel.Dispatcher;  
  6. using System.ServiceModel.Description;  
  7. using System.ServiceModel.Channels;  
  8. using System.ServiceModel.Configuration;  
  9. namespace BNCommon.ServiceHelper  
  10. ...{  
  11. public class BNServiceOperationBehavior : BehaviorExtensionElement, 
    IServiceBehavior  
  12. ...{  
  13. BehaviorExtensionElement成员#region BehaviorExtensionElement成员  
  14. public override Type BehaviorType  
  15. ...{  
  16. get ...{ return typeof(BNServiceOperationBehavior); }  
  17. }  
  18. protected override object CreateBehavior()  
  19. ...{  
  20. return new BNServiceOperationBehavior();  
  21. }  
  22. #endregion 

 

IServiceBehavior 成员#region IServiceBehavior 成员

 

  1. public void AddBindingParameters(ServiceDescription serviceDescription, 
    ServiceHostBase serviceHostBase, System.Collections.ObjectModel.
    Collection
    <ServiceEndpoint> endpoints, BindingParameterCollection
     bindingParameters)  
  2. ...{  
  3. return;  
  4. }  
  5. public void ApplyDispatchBehavior(ServiceDescription 
    serviceDescription, ServiceHostBase serviceHostBase)  
  6. ...{  
  7. foreach (ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)  
  8. ...{  
  9. chanDisp.ErrorHandlers.Add(new BNErrorHandler());  
  10. }  
  11. }  
  12. public void Validate(ServiceDescription serviceDescription, 
    ServiceHostBase serviceHostBase)  
  13. ...{  
  14. return;  
  15. }  
  16. #endregion  
  17. }  

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

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

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

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using BNCommon.ServiceHelper;  
  5. namespace BNClinicService.ServiceConsole  
  6. ...{  
  7. class Program  
  8. ...{  
  9. static void Main(string[] args)  
  10. ...{  
  11. System.Console.WriteLine("Press <ENTER> to start service.");  
  12. System.Console.ReadLine();  
  13. //订阅异常事件  
  14. BNCommon.ServiceHelper.BNServiceEvents.EventServiceMethodExecError += 
    new BNServiceEvents.HandleServiceMethodExecError
    (BNServiceEvents_EventServiceMethodExecError);  
  15. //启动服务  
  16. BNIIServiceLayer.SecurityServiceHosting.StartService();  
  17. System.Console.WriteLine("Press <ENTER> to stop service.");  
  18. System.Console.ReadLine();  
  19. //停止服务   
  20. BNIIServiceLayer.SecurityServiceHosting.StopService();  
  21. }   
  22. static void BNServiceEvents_EventServiceMethodExecError(Exception ex)  
  23. ...{  
  24. //写日志....  
  25. BNIVSericeLayer.BNServiceLogEvent.FireLogEvent(BNIVSericeLayer.
    LogHelper.GetFaultLogModel(ex.Source, string.Empty, ex.Message, string.Empty));  
  26. //其他处理....  
  27. }  
  28. }  

以上就是对WCF全局错误捕获的相关介绍。

责任编辑:曹凯 来源: CSDN
相关推荐

2010-02-23 10:51:32

WCF Address

2010-02-26 10:46:12

WCF行为扩展

2010-02-26 08:59:10

WCF服务宿主程序

2010-12-21 14:08:50

PowerShell

2010-02-22 10:42:12

WCF Stream

2009-12-21 18:32:22

关闭WCF链接

2010-02-22 14:28:35

WCF实现loadin

2009-12-08 14:10:55

Silverlight

2010-02-26 14:05:57

WCF通信方式

2010-02-24 15:28:59

WCF ABC

2015-02-03 14:45:55

android全局异常

2010-02-23 17:59:52

WSIT连接WCF

2010-02-24 10:07:48

WCF跨越边界

2010-03-02 09:32:54

WCF服务消息

2010-02-22 14:09:08

WCF Dispose

2010-03-02 16:05:48

WCF端点配置

2010-02-25 16:07:28

WCF REST

2010-02-26 17:44:51

WCF安全参数

2010-02-23 17:05:38

2010-02-25 10:10:29

WCF使用Header
点赞
收藏

51CTO技术栈公众号