Useful WCF Behaviors - IErrorHandler
Behaviors in WCF are so stinking useful, and once you get past the basics of WCF they're arguably a necessity. Microsoft has saved itself from hundreds of hours of cursing by including the ability to define custom behaviors.
My favorite use of behaviors is addressing some cross cutting concerns that we consistently have in our WCF services. Specifically logging, service registration, wsdl tweaking, and error handling. Which brings us around to the IErrorHandler interface.
Implementing IErrorHandler "Allows an implementer to control the fault message returned to the caller and optionally perform custom error processing such as logging". Thank you MSDN. IErrorHandler has two methods to implement: HandleError, and ProvideFault. Implement them so that they do what they say they do. HandleError should do something with the error (in our case, we log it) and ProvideFault should return a fault message.
#region IErrorHandler Members
// HandleError. Log an error, then allow the error to be handled as usual.
// Return true if the error is considered as already handled
public bool HandleError(Exception error)
{
try
{
\\our loggin service
LoggerClient logger = new LoggerClient(basic, logAddress);
\\build log message
StringBuilder err = new StringBuilder();
err.AppendLine("Source: " + error.Source);
err.AppendLine("Target: " + error.TargetSite.ToString());
err.AppendLine("Message: " + error.Message);
err.AppendLine("Stack Trace: " + error.StackTrace.ToString());
CustomLogEntry log = new CustomLogEntry();
log.AppDomainName = AppDomain.CurrentDomain.FriendlyName;
log.EventId = 900;
log.EventIdSpecified = true;
log.MachineName = Environment.MachineName;
log.Message = err.ToString();
log.Priority = 1;
log.PrioritySpecified = true;
log.Severity = TraceEventType.Error;
log.SeveritySpecified = true;
log.Title = "Exception Caught: " + error.Message;
logger.Log(log);
return true;
}
catch (Exception)
{
throw new Exception("error in the handler");
}
}
public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
{
// Shield the unknown exception
FaultException faultException = new FaultException(error.Message);
MessageFault messageFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault, faultException.Action);
}
#endregion
Pretty straight forward.
Now we just have to figure out a way to get this to apply to our service, preferably without a lot of extra work. So we don't want to go mucking around with attributes or anything else we have to remember to stick in as we're writing the code. That's where behaviors come in. You can apply them via the config file.
We know we want this particular behavior to apply to our entire service, so we want to implement IServiceBehavior in our class along with IErrorHandler. IServiceBehavior has four methods that we have to implement, but fortunately we can ignore three of them. The one we care about in this case is ApplyDispatchBehavior, where we will apply our error handler to each of the channels in the service. In the example below ErrorHandler is the name of the class we are implementing. Inventive name, isn't it?
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
IErrorHandler errorHandler = new ErrorHandler();
foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
channelDispatcher.ErrorHandlers.Add(errorHandler);
}
}
Great, that's done. Now there's only one thing left to do. We need a class that inherits from BehaviorExtensionElement so that we can add our behavior via configuration. Fortunately BehaviorExtensionElement is a breeze to implement. You need to impelement BehaviorType which just returns the type of your just implement IServiceBehavior class (ErrorHandler above), and CreateBehavior which returns an instantiation of the class.
class ErrorHandlerBehavior : BehaviorExtensionElement
{
public override Type BehaviorType
{
get
{
return typeof(ErrorHandler);
}
} protected override object CreateBehavior()
{
return new ErrorHandler();
}
}
Now the code is done. The last thing to do is to apply the newly created behavior in the config file. Add our new BehaviorExtensionElement to the behaviorExtensions section, and then specify it in the serviceBehaviors section. One important caveat, if includeExceptionDetailInFaults is set to true, then the exception shielding (and I believe Error Handling) will not work.
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="ErrorLogging" type="ErrorHandlerBehavior, ErrorHandling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8746502a48718374" />
</behaviorExtensions>
</extensions>
<bindings>
<basicHttpBinding>
<binding name="basicBinding">
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="Service1Behavior" name="Service">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="Service" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Service1Behavior">
<serviceMetadata httpGetUrl="" httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<ErrorLogging />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
There you go. Apply this to all of your WCF services and you won't have to re-invent the wheel again.
Useful WCF Behaviors - IErrorHandler的更多相关文章
- Wcf实现IServiceBehavior拓展机制
IServiceBehavior接口 描述:提供一种在整个服务内修改或插入自定义拓展机制: 命名空间: System.ServiceModel.Description程序集: System.Ser ...
- BizTalk 开发系列(四十) BizTalk WCF-SQL Adapter读取SQL Service Broker消息
SQL Service Broker 是在SQL Server 2005中新增的功能.Service Broker 为 SQL Server 提供队列和可靠的消息传递,可以可用来建立以异步消息为基础的 ...
- 在web.config里使用configSource分隔各类配置
转:http://www.yongfa365.com/Item/using-configSource-Split-Configs.html 大型项目中,可能有多个Service,也就是会有一堆配置,而 ...
- 【WCF】自定义错误处理(IErrorHandler接口的用法)
当被调用的服务操作发生异常时,可以直接把异常的原始内容传回给客户端.在WCF中,服务器传回客户端的异常,通常会使用 FaultException,该异常由这么几个东东组成: 1.Action:在服务调 ...
- WCF扩展系列 - 行为扩展(Behaviors)
原文地址:http://www.cnblogs.com/Creator/archive/2011/05/21/2052687.html 这个系列的第一部分将会重点关注WCF行为(behaviors), ...
- 【WCF】错误处理(四):一刀切——IErrorHandler
前面几篇烂文中所介绍到的错误方式,都是在操作协定的实现代码中抛出 FaultException 或者带泛型参数的detail方案,有些时候,错误的处理方法比较相似,可是要每个操作协定去处理,似乎也太麻 ...
- 利用Attribute和IErrorHandler处理WCF全局异常
在处理WCF异常的时候,有大概几种方式: 第一种是在配置文件中,将includeExceptionDetailInFaults设置为true <behavior name="servi ...
- 【转】WCF扩展系列 - 行为扩展(Behaviors)
原文:https://www.cnblogs.com/Creator/archive/2011/05/21/2052687.html 这个系列的第一部分将会重点关注WCF行为(behaviors),W ...
- WCF入门(22)
前言 本还想写一集WCF入门教程的,心情实在不好,明天又还有面试,改天再写吧. 说一下今天遇到的入职坑.面试能坑,上班能坑,完全没想到入职也能坑.切身经历. 今年10月份想换工作,更新了一下简历,接到 ...
随机推荐
- VS2010,VS2012,VS2015等的自动提示不能默认选中的功能解决办法
很简单,只需要按 ctrl+alt+space 即可切换. 蛋疼,我到底为什么总会不小心切换过去,而且每次都记不住这个快捷键切换回来...
- Js框架设计之DomReady
一.在介绍DomReady之前,先了解下相关的知识 1.HTML是一种标记语言,告诉我们这页面里面有什么内容,但是行为交互则要通过DOM操作来实现,但是注意:不要把尖括号里面的内容看作是DOM! 2. ...
- xamarin RunOnUiThread
One of the keys to maintaining a responsive GUI is to do long-running tasks on a background thread s ...
- 级联sql
select ID, PID, NAME,KEY from HS_DICT start with KEY = 'HS_EXP_WORK_LOCATION'connect by prior ID = P ...
- SP16580 QTREE7 - Query on a tree VII
Description 一棵树,每个点初始有个点权和颜色(0/1) 0 u :询问所有u,v 路径上的最大点权,要满足u,v 路径上所有点的颜色都相同 1 u :反转u 的颜色 2 u w :把u 的 ...
- 【Bigdecimal】
---恢复内容开始--- 大位数除法的时候注意1/3问题:异常:[Exception in thread "main" java.lang.ArithmeticException: ...
- [转]浅谈 .NET Framework 与 .NET Core 的区别与联系
本文转自:http://www.cnblogs.com/huchaoheng/p/6295688.html 2017到了,咱们学点啥啊,要想知道学点啥,先弄清.NET Framework 与 .NET ...
- Bitbucket 关联 VS
1.双击已经建立好的仓库 - 克隆仓库-目标路径选择一个空的文件夹,点击克隆 2.把已经建立好的项目拷贝的到刚刚那个空目录里面 3.在VS里面打开新路径下面的项目,点击提交即可; 我安装了Bitbuc ...
- Autofac +webapi 配置
Autofac配置 using Autofac; using System; using System.Collections.Generic; using System.Linq; using Sy ...
- 版本控制器之SVN
1.开发中的实际问题 1.1 小明负责的模块就要完成了,就在即将Release之前的一瞬间,电脑突然蓝屏,硬盘光荣牺牲!几个月来的努力付之东流——需求之一:备份! 1.2 这个项目中需要一个很复杂的功 ...