服务端增加WCF服务全局异常处理机制,任一WCF服务或接口方式出现异常,将统一调用WCF_ExceptionHandler.ProvideFault方法,因此不需要每个方法使用try catch写法。

 1 /// <summary>
2 /// WCF服务端异常处理器
3 /// </summary>
4 public class WCF_ExceptionHandler : IErrorHandler
5 {
6 #region IErrorHandler Members
7
8 /// <summary>
9 /// HandleError
10 /// </summary>
11 /// <param name="ex">ex</param>
12 /// <returns>true</returns>
13 public bool HandleError(Exception ex)
14 {
15 return true;
16 }
17
18 /// <summary>
19 /// ProvideFault
20 /// </summary>
21 /// <param name="ex">ex</param>
22 /// <param name="version">version</param>
23 /// <param name="msg">msg</param>
24 public void ProvideFault(Exception ex, MessageVersion version, ref Message msg)
25 {
26 //
27 //在这里处理服务端的消息,将消息写入服务端的日志
28 //
29 string err = string.Format("调用WCF接口 '{0}' 出错", ex.TargetSite.Name) ",详情:\r\n" ex.Message;
30 var newEx = new FaultException(err);
31
32 MessageFault msgFault = newEx.CreateMessageFault();
33 msg = Message.CreateMessage(version, msgFault, newEx.Action);
34 }
35
36 #endregion
37 }
38
 1 /// <summary>
2 /// WCF服务类的特性
3 /// </summary>
4 public class WCF_ExceptionBehaviourAttribute : Attribute, IServiceBehavior
5 {
6 private readonly Type _errorHandlerType;
7
8 public WCF_ExceptionBehaviourAttribute(Type errorHandlerType)
9 {
10 _errorHandlerType = errorHandlerType;
11 }
12
13 #region IServiceBehavior Members
14
15 public void Validate(ServiceDescription description,
16 ServiceHostBase serviceHostBase)
17 {
18 }
19
20 public void AddBindingParameters(ServiceDescription description,
21 ServiceHostBase serviceHostBase,
22 Collection<ServiceEndpoint> endpoints,
23 BindingParameterCollection parameters)
24 {
25 }
26
27 public void ApplyDispatchBehavior(ServiceDescription description,
28 ServiceHostBase serviceHostBase)
29 {
30 var handler =
31 (IErrorHandler)Activator.CreateInstance(_errorHandlerType);
32
33 foreach (ChannelDispatcherBase dispatcherBase in
34 serviceHostBase.ChannelDispatchers)
35 {
36 var channelDispatcher = dispatcherBase as ChannelDispatcher;
37 if (channelDispatcher != null)
38 channelDispatcher.ErrorHandlers.Add(handler);
39 }
40 }
41
42 #endregion
43 }
44

使用:

1 [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
2 [WCF_ExceptionBehaviour(typeof(WCF_ExceptionHandler))]
3 public class AccountModuleService : IAccountModuleService
4 {
5
6
7 }

WCF服务全局异常处理机制的更多相关文章

  1. 服务端增加WCF服务全局异常处理机制

    服务端增加WCF服务全局异常处理机制,任一WCF服务或接口方式出现异常,将统一调用WCF_ExceptionHandler.ProvideFault方法,因此不需要每个方法使用try catch写法. ...

  2. WCF服务全局统一异常处理机制

    转载:http://www.csframework.com/archive/1/arc-1-20150109-2193.htm 服务端增加WCF服务全局异常处理机制,任一WCF服务或接口方式出现异常, ...

  3. ASP.NET Core 中间件的使用(三):全局异常处理机制

    前言 我们经常听到"秒修复秒上线",觉得很厉害的样子. 其实不然,这只是一个调侃而已,出现问题的方式很多(逻辑漏洞.代码异常.操作方式不正确等). 我们今天来说代码异常问题怎么快速 ...

  4. Spring Boot 2.x 系列教程:WebFlux REST API 全局异常处理 Error Handling

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本文内容 为什么要全局异常处理? WebFlux REST 全 ...

  5. Spring Boot 2 Webflux的全局异常处理

    https://www.jianshu.com/p/6f631f3e00b9 本文首先将会回顾Spring 5之前的SpringMVC异常处理机制,然后主要讲解Spring Boot 2 Webflu ...

  6. SpringMVC 全局异常处理

    在 JavaEE 项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合度 ...

  7. Spring Cloud微服务如何设计异常处理机制?

    导读 今天和大家聊一下在采用Spring Cloud进行微服务架构设计时,微服务之间调用时异常处理机制应该如何设计的问题.我们知道在进行微服务架构设计时,一个微服务一般来说不可避免地会同时面向内部和外 ...

  8. WCF全局异常处理

    在用wcf做为单纯的服务端的时候,发生错误是常有的事情,特别是在调用其他系统提供的接口的时候,发生的一些错误总是让人摸不着头脑,严重影响了错误的定位.做.net web开发的时候,我们可以在Globa ...

  9. 花了三天整理,Spring Cloud微服务如何设计异常处理机制?还看不懂算我输

    前言 首先说一下为什么发这篇文章,是这样的.之前和粉丝聊天的时候有聊到在采用Spring Cloud进行微服务架构设计时,微服务之间调用时异常处理机制应该如何设计的问题.我们知道在进行微服务架构设计时 ...

随机推荐

  1. [POI2008]MAF-Mafia(图论,贪心)

    题目描述 Mob feud rages in Equatorial Byteotia. The mob bosses have come to the country's capital, Byteb ...

  2. 【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)

    Problem Description Consider a un-rooted tree T which is not the biological significance of tree or ...

  3. 路由器基础设置之ospf

    我们将以上面的拓扑图来进行实验,要用ospf的协议达到全网互通的效果 router1: enable 进入特权模式 config t 进入全局配置模式 interface L0 ip address ...

  4. ethereum(以太坊)(基础)--容易忽略的坑(三)

    pragma solidity ^0.4.10; contract Byte{ bytes [] public T=new bytes[](3); function setLeng(uint len) ...

  5. MySQL5.7版本安装

    安装方式一: ZIP压缩包安装 >>>首先,到MYSQL官网下载.zip格式的MySQL Server的压缩包,根据需要选择x86或x64版. >>>下载需要登录o ...

  6. 堆数据结构(heapq)简单应用

    ## 堆数据结构(heapq)简单应用 # 堆数据结构 heapq # 常用方法:nlargest(),nsmallest(),heapify(),heappop() # 如果需要的个数较小,使用nl ...

  7. Python学习 :深浅拷贝

    深浅拷贝 一.浅拷贝 只拷贝第一层数据(不可变的数据类型),并创建新的内存空间进行储蓄,例如:字符串.整型.布尔 除了字符串以及整型,复杂的数据类型都使用一个共享的内存空间,例如:列表 列表使用的是同 ...

  8. html 弹框 优化 alert

    <!DOCTYPE html> <html> <head> <title>cs</title> </head> <styl ...

  9. 网站安全检测 漏洞检测 对thinkphp通杀漏洞利用与修复建议

    thinkphp在国内来说,很多站长以及平台都在使用这套开源的系统来建站,为什么会这么深受大家的喜欢,第一开源,便捷,高效,生成静态化html,第二框架性的易于开发php架构,很多第三方的插件以及第三 ...

  10. 002---Python基本数据类型--字符串

    字符串 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1p ...