WCF服务全局统一异常处理机制
转载:http://www.csframework.com/archive/1/arc-1-20150109-2193.htm
服务端增加WCF服务全局异常处理机制,任一WCF服务或接口方式出现异常,将统一调用WCF_ExceptionHandler.ProvideFault方法,因此不需要每个方法使用try catch写法。
C# Code:
/// <summary>
/// WCF服务端异常处理器
/// </summary>
public class WCF_ExceptionHandler : IErrorHandler
{
#region IErrorHandler Members
/// <summary>
/// HandleError
/// </summary>
/// <param name="ex">ex</param>
/// <returns>true</returns>
public bool HandleError(Exception ex)
{
return true;
}
/// <summary>
/// ProvideFault
/// </summary>
/// <param name="ex">ex</param>
/// <param name="version">version</param>
/// <param name="msg">msg</param>
public void ProvideFault(Exception ex, MessageVersion version, ref Message msg)
{
//
//在这里处理服务端的消息,将消息写入服务端的日志
//
string err = string.Format("调用WCF接口 '{0}' 出错", ex.TargetSite.Name) ",详情:\r\n" ex.Message;
var newEx = new FaultException(err);
MessageFault msgFault = newEx.CreateMessageFault();
msg = Message.CreateMessage(version, msgFault, newEx.Action);
}
#endregion
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <summary>
/// WCF服务端异常处理器
/// </summary>
public class WCF_ExceptionHandler : IErrorHandler
{
#region IErrorHandler Members
/// <summary>
/// HandleError
/// </summary>
/// <param name="ex">ex</param>
/// <returns>true</returns>
public bool HandleError(Exception ex)
{
return true;
}
/// <summary>
/// ProvideFault
/// </summary>
/// <param name="ex">ex</param>
/// <param name="version">version</param>
/// <param name="msg">msg</param>
public void ProvideFault(Exception ex, MessageVersion version, ref Message msg)
{
//
//在这里处理服务端的消息,将消息写入服务端的日志
//
string err = string.Format("调用WCF接口 '{0}' 出错", ex.TargetSite.Name) ",详情:\r\n" ex.Message;
var newEx = new FaultException(err);
MessageFault msgFault = newEx.CreateMessageFault();
msg = Message.CreateMessage(version, msgFault, newEx.Action);
}
#endregion
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
C# Code:
/// <summary>
/// WCF服务类的特性
/// </summary>
public class WCF_ExceptionBehaviourAttribute : Attribute, IServiceBehavior
{
private readonly Type _errorHandlerType;
public WCF_ExceptionBehaviourAttribute(Type errorHandlerType)
{
_errorHandlerType = errorHandlerType;
}
#region IServiceBehavior Members
public void Validate(ServiceDescription description,
ServiceHostBase serviceHostBase)
{
}
public void AddBindingParameters(ServiceDescription description,
ServiceHostBase serviceHostBase,
Collection<ServiceEndpoint> endpoints,
BindingParameterCollection parameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription description,
ServiceHostBase serviceHostBase)
{
var handler =
(IErrorHandler)Activator.CreateInstance(_errorHandlerType);
foreach (ChannelDispatcherBase dispatcherBase in
serviceHostBase.ChannelDispatchers)
{
var channelDispatcher = dispatcherBase as ChannelDispatcher;
if (channelDispatcher != null)
channelDispatcher.ErrorHandlers.Add(handler);
}
}
#endregion
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <summary>
/// WCF服务类的特性
/// </summary>
public class WCF_ExceptionBehaviourAttribute : Attribute, IServiceBehavior
{
private readonly Type _errorHandlerType;
public WCF_ExceptionBehaviourAttribute(Type errorHandlerType)
{
_errorHandlerType = errorHandlerType;
}
#region IServiceBehavior Members
public void Validate(ServiceDescription description,
ServiceHostBase serviceHostBase)
{
}
public void AddBindingParameters(ServiceDescription description,
ServiceHostBase serviceHostBase,
Collection<ServiceEndpoint> endpoints,
BindingParameterCollection parameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription description,
ServiceHostBase serviceHostBase)
{
var handler =
(IErrorHandler)Activator.CreateInstance(_errorHandlerType);
foreach (ChannelDispatcherBase dispatcherBase in
serviceHostBase.ChannelDispatchers)
{
var channelDispatcher = dispatcherBase as ChannelDispatcher;
if (channelDispatcher != null)
channelDispatcher.ErrorHandlers.Add(handler);
}
}
#endregion
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
使用:
C# Code:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[WCF_ExceptionBehaviour(typeof(WCF_ExceptionHandler))]
public class AccountModuleService : IAccountModuleService
{
//来源:C/S框架网(www.csframework.com) QQ:1980854898
}
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET