WCF全局錯誤捕獲正確內容解析
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ā)該事件,代碼如下:
- 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(HandleServiceMethod
ExecErrorKey, value); }- remove ...{ listEventDelegates.RemoveHandler(HandleServiceMethod
ExecErrorKey, value); }- }
- public static void FireEventServiceMethodExecError(Exception ex)
- ...{
- HandleServiceMethodExecError handler = (HandleServiceMethodExecError)
listEventDelegates[HandleServiceMethodExecErrorKey];- if (handler != null)
- ...{
- handler(ex);
- }
- }
- }
- }
增加一個類實現(xiàn)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 成員#region IErrorHandler 成員
- public bool HandleError(Exception error)
- ...{
- //異常發(fā)生時觸發(fā)事件
- BNServiceEvents.FireEventServiceMethodExecError(error);
- return true;
- }
- public void ProvideFault(Exception error, MessageVersion
version, ref Message fault)- ...{
- }
- #endregion
- }
- }
增加一個類實現(xiàn)System.ServiceModel.Description.IServiceBehavior接口并繼承System.ServiceModel.Configuration.BehaviorExtensionElement用于將WCF全局錯誤捕獲行為加入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成員#region BehaviorExtensionElement成員
- public override Type BehaviorType
- ...{
- get ...{ return typeof(BNServiceOperationBehavior); }
- }
- protected override object CreateBehavior()
- ...{
- return new BNServiceOperationBehavior();
- }
- #endregion
IServiceBehavior 成員#region IServiceBehavior 成員
- public void AddBindingParameters(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase, System.Collections.ObjectModel.
Collection<ServiceEndpoint> endpoints, BindingParameterCollection
bindingParameters)- ...{
- return;
- }
- public void ApplyDispatchBehavior(ServiceDescription
serviceDescription, ServiceHostBase serviceHostBase)- ...{
- foreach (ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
- ...{
- chanDisp.ErrorHandlers.Add(new BNErrorHandler());
- }
- }
- public void Validate(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase)- ...{
- return;
- }
- #endregion
- }
- }
在實例化ServiceHost時將擴展的Service行為加入行為集合中(也可以通過配置文件的方式實現(xiàn),這里使用代碼實現(xiàn)):
- 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));- //其他處理....
- }
- }
- }
以上就是對WCF全局錯誤捕獲的相關介紹。


2010-02-26 10:46:12
2009-12-21 18:32:22
2010-02-26 14:05:57
2010-03-02 09:32:54
2010-02-23 17:05:38
2010-02-25 16:07:28
2010-03-01 16:59:31
2010-03-02 16:05:48




