WCF全局错误捕获正确内容解析
创始人
2024-06-23 19:10:55
0

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 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  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  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全局错误捕获的相关介绍。

相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
粉嫩如何诠释霸道 东芝M805... “霸道粉”是个什么玩意东芝M805拿过来的时候,笔者扑哧笑了,不是笑这款笔记本,而是笑这款产品的颜色...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
全面诠释网络负载均衡 负载均衡的出现大大缓解了服务器的压力,更是有效的利用了资源,提高了效率。那么我们现在来说一下网络负载...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
30分钟搞定iOS自定义相机 最近公司的项目中用到了相机,由于不用系统的相机,UI给的相机切图,必须自定义才可以。就花时间简单研究...
Intel将Moblin社区控... 本周二,非营利机构Linux基金会宣布,他们将担负起Moblin社区的管理工作,而这之前,Mobli...