自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF全局錯誤捕獲正確內容解析

開發(fā) 開發(fā)工具
WCF全局錯誤捕獲在我們的實際應用中將會給大家?guī)碓S多幫助。在這里我們將會針對這一應用技巧給大家?guī)硪恍椭?/div>

WCF開發(fā)插件的利用,為我們的程序開發(fā)實現(xiàn)了許多新的功能。而且在處理錯誤異常的時候,表現(xiàn)尤為突出。在這里我們將會為大家詳細介紹一下有關WCF全局錯誤捕獲的相關內容,希望對大家有所幫助。#t#

在 Web Applications中我們可以在Global.asax中通過Application_Error捕獲應用程序錯誤。在ASMX Web Services中我們可以寫一個Soap Extension在程序異常被發(fā)送到客戶端之前將其捕獲并進行處理。

如果想在WCF中實現(xiàn)以下功能,當Server端程序出現(xiàn)異常時,程序可以捕獲所有異常并進行寫日志、通知管理員等處理。我們可以為每個Server端的方法加入try....catch...finally塊,但這樣寫太麻煩。

實際上,在WCF中我們可以通過以下方式實現(xiàn)WCF全局錯誤捕獲:

1 MSDN中講到,在System.ServiceModel.Dispatcher命名空間下有個IErrorHandler 接口。允許實施者對返回給調用方的錯誤消息進行控制,還可以選擇執(zhí)行自定義錯誤處理,例如日志記錄。

2 實現(xiàn)方法示例:(以下示例僅僅按最簡單的方式去實現(xiàn)WCF全局錯誤捕獲)

定義一個類包含靜態(tài)事件用于發(fā)生錯誤時觸發(fā)該事件,代碼如下:

  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. }  

 

增加一個類實現(xiàn)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. //異常發(fā)生時觸發(fā)事件  
  17. BNServiceEvents.FireEventServiceMethodExecError(error);  
  18. return true;  
  19. }  
  20. public void ProvideFault(Exception error, MessageVersion
     version, ref Message fault)  
  21. ...{  
  22. }  
  23. #endregion  
  24. }  

 

增加一個類實現(xiàn)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行為加入行為集合中(也可以通過配置文件的方式實現(xiàn),這里使用代碼實現(xiàn)):

  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

2009-12-21 18:32:22

關閉WCF鏈接

2010-02-22 10:42:12

WCF Stream

2010-02-22 14:28:35

WCF實現(xiàn)loadin

2010-02-26 14:05:57

WCF通信方式

2009-12-08 14:10:55

Silverlight

2010-02-24 15:28:59

WCF ABC

2010-02-23 17:59:52

WSIT連接WCF

2010-02-24 10:07:48

WCF跨越邊界

2010-03-02 09:32:54

WCF服務消息

2010-02-23 17:05:38

2010-02-25 16:07:28

WCF REST

2010-02-26 09:33:18

WCF創(chuàng)建WebSer

2010-02-22 14:09:08

WCF Dispose

2010-02-26 17:44:51

WCF安全參數(shù)

2010-03-01 16:59:31

WCF異常調試

2010-03-02 16:05:48

WCF端點配置
點贊
收藏

51CTO技術棧公眾號