日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
WCF全局錯(cuò)誤捕獲正確內(nèi)容解析

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

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

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

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

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

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

定義一個(gè)類包含靜態(tài)事件用于發(fā)生錯(cuò)誤時(shí)觸發(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. }  

增加一個(gè)類實(shí)現(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ā)生時(shí)觸發(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. }  

增加一個(gè)類實(shí)現(xiàn)System.ServiceModel.Description.IServiceBehavior接口并繼承System.ServiceModel.Configuration.BehaviorExtensionElement用于將WCF全局錯(cuò)誤捕獲行為加入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. }  

在實(shí)例化ServiceHost時(shí)將擴(kuò)展的Service行為加入行為集合中(也可以通過配置文件的方式實(shí)現(xiàn),這里使用代碼實(shí)現(xiàn)):

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

在宿主程序中訂閱BNServiceEvents.EventServiceMethodExecError事件進(jìn)行處理,代碼如下:

 
 
 
  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. //啟動(dòng)服務(wù)  
  16. BNIIServiceLayer.SecurityServiceHosting.StartService();  
  17. System.Console.WriteLine("Press  to stop service.");  
  18. System.Console.ReadLine();  
  19. //停止服務(wù)   
  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. }  

以上就是對(duì)WCF全局錯(cuò)誤捕獲的相關(guān)介紹。


標(biāo)題名稱:WCF全局錯(cuò)誤捕獲正確內(nèi)容解析
URL鏈接:http://www.5511xx.com/article/cojoseh.html