/// <summary>
/// WCF服务端异常处理器
/// </summary>
public class WCF_ExceptionHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return true;
}

public void ProvideFault(Exception ex, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message msg)
{

//
//在这里处理服务端的消息,将消息写入服务端的日志
//
string err = string.Format("调用WCF接口 '{0}' 出错", ex.TargetSite.Name) + ",详情:\r\n" + ex.Message;
Logger.LOG_ERROR.Error(err + ",ex=" + ex);
var newEx = new FaultException(err);

MessageFault msgFault = newEx.CreateMessageFault();
msg = Message.CreateMessage(version, msgFault, newEx.Action);
}
}

/// <summary>
/// WCF服务类的错误的特性
/// </summary>
public class WCF_ExceptionBehaviourAttribute : Attribute, IServiceBehavior
{
private readonly List<Type> _errorHandlerTypelist;

/// <summary>
/// 注入单个的错误事件
/// </summary>
/// <param name="errorHandlerType"></param>
public WCF_ExceptionBehaviourAttribute(Type errorHandlerType)
{
_errorHandlerTypelist = new List<Type>() { errorHandlerType };
}
/// <summary>
/// 注入多个的自定义错误
/// </summary>
/// <param name="errorHandlerTypelist"></param>
public WCF_ExceptionBehaviourAttribute(List<Type> errorHandlerTypelist)
{
_errorHandlerTypelist = errorHandlerTypelist;
}

#region IServiceBehavior Members
/// <summary>
/// 验证服务主机和服务描述
/// </summary>
/// <param name="description"></param>
/// <param name="serviceHostBase"></param>
public void Validate(ServiceDescription description,
ServiceHostBase serviceHostBase)
{
Logger.LOG_INFO.Info(description.Name + "开启WCF异常捕获监听");
}

/// <summary>
/// 绑定参数
/// </summary>
/// <param name="description"></param>
/// <param name="serviceHostBase"></param>
/// <param name="endpoints"></param>
/// <param name="parameters"></param>
public void AddBindingParameters(ServiceDescription description,
ServiceHostBase serviceHostBase,
Collection<ServiceEndpoint> endpoints,
BindingParameterCollection parameters)
{
}
/// <summary>
/// 应用调度-更改运行时属性值或插入自定义的功能
/// </summary>
/// <param name="description"></param>
/// <param name="serviceHostBase"></param>
public void ApplyDispatchBehavior(ServiceDescription description,
ServiceHostBase serviceHostBase)
{
if (_errorHandlerTypelist != null)
{
foreach (var _errorHandlerType in _errorHandlerTypelist)
{
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
}

使用

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[WCF_ExceptionBehaviour(typeof(WCF_ExceptionHandler))]
public class AccountModuleService : IAccountModuleService
{

}

如果WCF多,添加到Base里面即可,不用每个wcf都加,有一个Base类添加一遍就可以了。

引用地址:转至博客:J.Y

最新文章

  1. 《A Convolutional Neural Network Cascade for Face Detection》
  2. 跨平台运行ASP.NET Core 1.0
  3. PHP include语句和require语句
  4. poj 1845 数论综合
  5. MFC加载皮肤 转自:http://www.cctry.com/thread-4032-1-1.html
  6. effective c++ (一)
  7. HDOJ1002题A + B Problem II,2个大数相加
  8. innodb的innodb_buffer_pool_size和MyISAM的key_buffer_size
  9. osip及eXosip的编译方法
  10. 移动App測试实战:顶级互联网企业软件測试和质量提升最佳实践
  11. oracle物化视图使用+hibernate
  12. Python学习笔记——进阶篇【第九周】———线程、进程、协程篇(队列Queue和生产者消费者模型)
  13. 2017 Multi-University Training Contest - Team 9 1003&amp;&amp;HDU 6163 CSGO【计算几何】
  14. MarkDown思考
  15. 前端开发JS白板编程题目若干
  16. 第二章 运算方法与运算器(浮点数的加减法,IEEE754标准32/64浮点规格化数)
  17. python collection模块
  18. python--列表,元组,字符串互相转换
  19. php 字符串固定长度,不够补充其他字符串
  20. &lt;Codis&gt;&lt;JedisPool&gt;&lt;DeadLock&gt;

热门文章

  1. Linux 系统安装 AutoFs 挂载服务
  2. GitHub 桌面版 v3.0 新特性「GitHub 热点速览」
  3. PHP反序列化链分析
  4. 【高并发】通过源码深度解析ThreadPoolExecutor类是如何保证线程池正确运行的
  5. docker使用详解
  6. 简单的 useState 实现
  7. 【Azure Developer】使用Azure PubSub服务示例代码时候遇见了.NET 6.0的代码转换问题
  8. Vue 中 watch 的一个坑
  9. Windows环境下启动Redis报错:Could not create server TCP listening socket 127.0.0.1:6379: bind: 操作成功完成。(已解决)
  10. Proxmox 7.2 部署 DoraCloud桌面云,支持vGPU