基于CallContextInitializer的WCF扩展导致的严重问题

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:

WCF是一个具有极高扩展度的分布式通信框架,无论是在信道层(Channel Layer)还是服务模型层(Service Model),我们都可以自定义相关组件通过相应的扩展注入到WCF运行环境中。在WCF众多可扩展点中ICallContextInitializer可以帮助我们在服务操作执行前后完成一些额外的功能,这实际上就是一种AOP的实现方式。比如在《通过WCF Extension实现Localization》中,我通过ICallContextInitializer确保了服务操作具有和客户端一样的语言文化;在《通过WCF Extension实现Context信息的传递》中,我通过ICallContextInitializer实现上下文在客户端到服务端的自动传递。ICallContextInitializer的定义如下:

   1: public interface ICallContextInitializer
   2: {
   3:     // Methods
   4:     void AfterInvoke(object correlationState);
   5:     object BeforeInvoke(InstanceContext instanceContext, IClientChannel channel, Message message);
   6: }

昨天,李永京同学问了我一个相关的问题。问题大概是这样的,他采用ICallContextInitializer实现WCF与NHibernate的集成。具体来说是通过ICallContextInitializer实现对事务 的提交,即通过BeforeInvoke方法初始化NHibernate的Session,通过AfterInvoke提交事务。但是,这中间具有一个挺严重的问题:当执行AfterInvoke提交事务的时候,是可能抛出异常的。一旦异常从AfterInvoke抛出,整个服务端都将崩溃。我们现在就来讨论一下这个问题,以及问题产生的根源。

一、问题重现

为了重现这个问题,我写了一个很简单的例子,你可以从这里下载该例子。首先我定义了如下一个实现了ICallContextInitializer接口的自定义CallContextInitializer:MyCallContextInitializer。在AfterInvoke方法中,我直接抛出一个异常。

   1: public class MyCallContextInitializer : ICallContextInitializer
   2: {
   3:     public void AfterInvoke(object correlationState)
   4:     {
   5:         throw new Exception("调用MyCallContextInitializer.AfterInvoke()出错!");
   6:     }
   7:  
   8:     public object BeforeInvoke(InstanceContext instanceContext, IClientChannel channel, Message message)
   9:     {
  10:         return null;
  11:     }
  12: }

然后,我们通过ServiceBehavior的方式来应用上面定义的MyCallContextInitializer。为此,我们定义了如下一个实现了IServiceBehavior接口的服务行为:MyServiceBehaviorAttribute。在ApplyDispatchBehavior方法中,将我们自定义的MyCallContextInitializer对象添加到所有终结点的分发运行时操作的CallContextInitializer列表中。

   1: public class MyServiceBehaviorAttribute : Attribute, IServiceBehavior
   2: {
   3:     public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { }
   4:  
   5:     public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
   6:     {
   7:         foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
   8:         {
   9:             foreach (EndpointDispatcher endpoint in dispatcher.Endpoints)
  10:             {
  11:                 foreach (DispatchOperation operation in endpoint.DispatchRuntime.Operations)
  12:                 {
  13:                     operation.CallContextInitializers.Add(new MyCallContextInitializer());
  14:                 }
  15:             }
  16:         }
  17:     }
  18:  
  19:     public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { }
  20: }

然后,我们采用我们熟悉的计算服务的例子来验证MyCallContextInitializer对整个服务端运行时的影响。下面是服务契约和服务类型的定义,我们自定义的服务行为MyServiceBehaviorAttribute通过自定义特性的方式应用到CalculatorService上面。

   1: namespace Artech.Exception2CallContextInitializer.Contracts
   2: {
   3:     [ServiceContract(Namespace="http://www.artech.com/")]
   4:     public interface ICalculator
   5:     {
   6:         [OperationContract]
   7:         double Add(double x, double y);
   8:     }
   9: }
   1: namespace Artech.Exception2CallContextInitializer.Services
   2: {
   3:     [MyServiceBehavior]
   4:     public class CalculatorService:ICalculator
   5:     {
   6:         public double Add(double x, double y)
   7:         {
   8:             return x + y;
   9:         }
  10:     }
  11: }

后然我们通过Console应用的方式来Host上面定义的CalculatorService,并创建另一个Console应用来模拟客户端对服务进行调用。由于相应的实现比较简单,在这里就不写出来了,对此不清楚的读者可以直接下载例子查看源代码。当你运行程序的时候,作为宿主的Console应用会崩溃,相应的进程也会被终止。如果服务宿主程序正常终止,客户端会抛出如左图所示的一个CommunicationException异常。

image

 

如果在调用超时时限内,服务宿主程序没能正常终止,客户端则会抛出如右图所示的TimeoutException异常。

image查看Event Log,你会发现两个相关的日志。它们的Source分别是:System.ServiceMode 3.0.0.0和.NET Runtime。两条日志相应的内容如下。如果你足够细心,你还会从中看到WCF一个小小的BUG。日志内容的第二行为“Message: ICallContextInitializer.BeforeInvoke threw an exception of type System.Exception: 调用MyCallContextInitializer.AfterInvoke()出错!”,实际上这里的“ICallContextInitializer.BeforeInvoke”应该改成“ICallContextInitializer.AfterInvoke”。下面一部分中你将会看到这个BUG是如何产生的。

FailFast was invoked.
 Message: ICallContextInitializer.BeforeInvoke threw an exception of type System.Exception: 调用MyCallContextInitializer.AfterInvoke()出错!
 Stack Trace:    at System.ServiceModel.Diagnostics.ExceptionUtility.TraceFailFast(String message, EventLogger logger)
   at System.ServiceModel.Diagnostics.ExceptionUtility.TraceFailFast(String message)
   at System.ServiceModel.DiagnosticUtility.FailFast(String message)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.UninitializeCallContextCore(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.UninitializeCallContext(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Dispatch(MessageRpc& rpc, Boolean isOperationContextSet)
   at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext)
   at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext)
   at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result)
   at System.ServiceModel.Dispatcher.ChannelHandler.OnAsyncReceiveComplete(IAsyncResult result)
   at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
   at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously)
   at System.ServiceModel.Channels.InputQueue`1.AsyncQueueReader.Set(Item item)
   at System.ServiceModel.Channels.InputQueue`1.EnqueueAndDispatch(Item item, Boolean canDispatchOnThisThread)
   at System.ServiceModel.Channels.InputQueue`1.EnqueueAndDispatch(T item, ItemDequeuedCallback dequeuedCallback, Boolean canDispatchOnThisThread)
   at System.ServiceModel.Channels.InputQueueChannel`1.EnqueueAndDispatch(TDisposable item, ItemDequeuedCallback dequeuedCallback, Boolean canDispatchOnThisThread)
   at System.ServiceModel.Channels.SingletonChannelAcceptor`3.Enqueue(QueueItemType item, ItemDequeuedCallback dequeuedCallback, Boolean canDispatchOnThisThread)
   at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, ItemDequeuedCallback callback)
   at System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContextCore(IAsyncResult result)
   at System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContext(IAsyncResult result)
   at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
   at System.Net.ListenerAsyncResult.WaitCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
 
 Process Name: Artech.Exception2CallContextInitializer.Services
 Process ID: 7652
.NET Runtime version 2.0.50727.4927 - ICallContextInitializer.BeforeInvoke threw an exception of type System.Exception: 调用MyCallContextInitializer.AfterInvoke()出错!

如果你想从消息交换得角度进一步剖析问题的本质,你可以采用Fiddler这样的工具。如果你真的这样做的话,你会发现服务端没有任何消息返回到客户端。

二、原因剖析

从上面表现出来的现象,我们可以知道这是一个非常严重的问题,因为它将会终止整个服务宿主进程。那么,是什么导致了这个严重的问题呢?实际上,如果通过Reflector对WCF相关代码进行反射,你将会很容易找到问题的根源。

ICallContextInitializer的AfterInvoke方法的最终是通过定义在DispatchOperationRuntime类型的一个命名为UninitializeCallContextCore的私有方法中被调用的。下面就是该方法的定义:

   1: private void UninitializeCallContextCore(ref MessageRpc rpc)
   2: {
   3:     object proxy = rpc.Channel.Proxy;
   4:     int callContextCorrelationOffset = this.Parent.CallContextCorrelationOffset;
   5:     try
   6:     {
   7:         for (int i = this.CallContextInitializers.Length - 1; i >= 0; i--)
   8:         {
   9:             this.CallContextInitializers[i].AfterInvoke(rpc.Correlation[callContextCorrelationOffset + i]);
  10:         }
  11:     }
  12:     catch (Exception exception)
  13:     {
  14:         DiagnosticUtility.FailFast(string.Format(CultureInfo.InvariantCulture, "ICallContextInitializer.BeforeInvoke threw an exception of type {0}: {1}", new object[] { exception.GetType(), exception.Message }));
  15:     }
  16: }
  17:  
  18:  
  19:  
  20:  

通过上面的代码,你会看到对DispatchOperation所有CallContextInitializer的AfterInvoke方法的调用是放在一个Try/Catch中进行的。当异常抛出后,会调用DiagnosticUtility的FailFast方法。传入该方法的是异常消息,你可以看到这里指定的消息是不对的,“ICallContextInitializer.BeforeInvoke”应该是“ICallContextInitializer.AfterInvoke”,这就是为什么你在Event Log看到日志内容是不准确的真正原因。我们进一步来看看FailFast的定义:

   1: [MethodImpl(MethodImplOptions.NoInlining)]
   2: internal static Exception FailFast(string message)
   3: {
   4:     try
   5:     {
   6:         try
   7:         {
   8:             ExceptionUtility.TraceFailFast(message);
   9:         }
  10:         finally
  11:         {
  12:             Environment.FailFast(message);
  13:         }
  14:     }
  15:     catch
  16:     {
  17:     }
  18:     Environment.FailFast(message);
  19:     return null;
  20: }

从上面的代码可以看到,整个过程分为两个步骤:对消息尽心Trace后调用Environment.FailFast方法。对Environment.FailFast方法具有一定了解的人应该之后,该方法执行后会终止掉当前进程。这就是为什么在ICallContextInitializer的AfterInvoke方法执行过程中出现未处理异常会导致宿主程序的非正常崩溃的真正原因。

三、总结

CallContextInitializer的设计可以看成是AOP在WCF中的实现,它可以在服务操作执行前后对方法调用进行拦截。你可以通过自定义CallContextInitializer实现一些服务操作执行前的初始化操作,以及操作执行后的清理工作。但是,当你自定义CallContextInitializer的时候,一定要确保AfterInvoke方法中没有异常抛出来。


作者:蒋金楠
微信公众账号:大内老A
微博: www.weibo.com/artech
如果你想及时得到个人撰写文章以及著作的消息推送,或者想看看个人推荐的技术资料,可以扫描左边二维码(或者长按识别二维码)关注个人公众号(原来公众帐号 蒋金楠的自媒体将会停用)。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
前端开发 .NET 开发框架
Wcf扩展
ASP.NET MVC和WCF真是微软两个很棒的框架,设计的很好,可扩展性非常强,到处都是横切、管道。 以前写过一篇MVC流程的文章,http://www.cnblogs.com/lovecindywang/archive/2010/12/02/1894740.html主要是使用了MVC的各种扩展。
789 0
|
.NET 网络架构
告别复杂WCF扩展 REST过程 ,让他 so easy
在看一些wcf的书和文章时,都会提到rest wcf ,实现方式多数通过对wcf进行一定程度的扩展实现的,实在是让我这种菜鸟心生畏惧~ 前些天为了体验mvc3安装了vs2010,顺便在在线模板里面搜索了一下wcf,没想到有意外发现,有图为证,这年代无图无真相   选择wcf后...
756 0
跟着Artech学习WCF扩展(3) 扩展CallContextInitializer
代码下载:点我   作为备份 Artech的代码要在他的博客里面找的 Artech大师级的“总分总”式的风格,的确是和高手过招用,作为一面菜鸟,纠结了老半天才明白,原来写博客也可以前后呼应 于是把这个小节点整理到一起已被以后不时只需     5 、自定义CallContextInitializer (Step 12 & Step 18) 提到CallContextInitializer,我想有一部分人会马上想到System.Runtime.Remoting.Messaging.CallContext。
750 0
|
信息无障碍
跟着Artech学习WCF扩展(4) 扩展MessageInspector
自定义MessageInspector 在client端和service端都有自己的MessageInspector,client端叫做ClientMessageInspector,实现System.ServiceModel.Dispatcher.IClientMessageInspector interface,而service端叫做 DispatchMessageInspector, 实现了System.ServiceModel.Dispatcher.IDispatchMessageInspector interface。
853 0
跟着Artech学习WCF扩展(1) Binding进行通信
这个demo简单 就一个服务器段的一个客户端的 主要是注释 Server的 using System; using System.Collections.Generic; using System.
696 0
跟着Artech学习WCF扩展(2) 自定义Channel与执行的顺序
源代码下载地址:点我 原文地址:http://www.cnblogs.com/artech/archive/2008/07/09/1238626.html 这节不看源码 看着真费劲哈   服务器端是这样的顺序 MyBindingElement.
707 0