新聞中心
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)方法示例:(以下示例僅僅按最簡(jiǎn)單的方式去實(shí)現(xiàn)WCF全局錯(cuò)誤捕獲)
定義一個(gè)類包含靜態(tài)事件用于發(fā)生錯(cuò)誤時(shí)觸發(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);
- }
- }
- }
- }
增加一個(gè)類實(shí)現(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ā)生時(shí)觸發(fā)事件
- BNServiceEvents.FireEventServiceMethodExecError(error);
- return true;
- }
- public void ProvideFault(Exception error, MessageVersion
version, ref Message fault)- ...{
- }
- #endregion
- }
- }
增加一個(gè)類實(shí)現(xiàn)System.ServiceModel.Description.IServiceBehavior接口并繼承System.ServiceModel.Configuration.BehaviorExtensionElement用于將WCF全局錯(cuò)誤捕獲行為加入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.
Collectionendpoints, 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
- }
- }
在實(shí)例化ServiceHost時(shí)將擴(kuò)展的Service行為加入行為集合中(也可以通過配置文件的方式實(shí)現(xiàn),這里使用代碼實(shí)現(xiàn)):
- ServiceHost sh = new ServiceHost(types[i]);
- sh.Description.Behaviors.Add(new BNServiceOperationBehavior());
在宿主程序中訂閱BNServiceEvents.EventServiceMethodExecError事件進(jìn)行處理,代碼如下:
- 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
to start service."); - System.Console.ReadLine();
- //訂閱異常事件
- BNCommon.ServiceHelper.BNServiceEvents.EventServiceMethodExecError +=
new BNServiceEvents.HandleServiceMethodExecError
(BNServiceEvents_EventServiceMethodExecError);- //啟動(dòng)服務(wù)
- BNIIServiceLayer.SecurityServiceHosting.StartService();
- System.Console.WriteLine("Press
to stop service."); - System.Console.ReadLine();
- //停止服務(wù)
- BNIIServiceLayer.SecurityServiceHosting.StopService();
- }
- static void BNServiceEvents_EventServiceMethodExecError(Exception ex)
- ...{
- //寫日志....
- BNIVSericeLayer.BNServiceLogEvent.FireLogEvent(BNIVSericeLayer.
LogHelper.GetFaultLogModel(ex.Source, string.Empty, ex.Message, string.Empty));- //其他處理....
- }
- }
- }
以上就是對(duì)WCF全局錯(cuò)誤捕獲的相關(guān)介紹。
網(wǎng)站題目:WCF全局錯(cuò)誤捕獲正確內(nèi)容解析
鏈接地址:http://www.fisionsoft.com.cn/article/cojoseh.html


咨詢
建站咨詢
