ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[上篇]

Action方法的执行具有两种基本的形式,即同步执行和异步执行,而在ASP.NETMVC的整个体系中涉及到很多同步/异步的执行方式,虽然在前面相应的文章中已经对此作了相应的介绍,为了让读者对此有一个整体的了解,我们来做一个总结性的论述。[本文已经同步到《How ASP.NET MVC Works?》中]

目录
一、MvcHandler的同步于异步
二、Controller的同步与异步
三、ActionInvoker的同步与异步
四、ControllerDescriptor的同步与异步
五、ActionDescriptor的同步与异步

一、MvcHandler的同步与异步

对于ASP.NET MVC应用来说,MvcHandler是最终用于处理请求的HttpHandler,它是通过UrlRoutingModule这个实现了URL路由的HttpModule被动态映射到相应的请求的。MvcHandler借助于ControllerFactory激活并执行目标Controller,并在执行结束后负责对激活的Controller进行释放,相关的内容请参与本书的第3章“Controller的激活”。如下面的代码片断所示,MvcHandler同时实现了IHttpHandler和IHttpAsyncHandler接口,所以它总是调用BeginProcessRequest/EndProcessRequest方法以异步的方式来处理请求

   1: public class MvcHandler : IHttpAsyncHandler, IHttpHandler, ...
   2: {
   3:     //其他成员   
   4:     IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData);
   5:     void IHttpAsyncHandler.EndProcessRequest(IAsyncResult result);
   6:     void IHttpHandler.ProcessRequest(HttpContext httpContext);
   7: }

二、Controller的同步与异步

Controller也具有同步与异步两个版本,它们分别实现了具有如下定义的两个接口IController和IAsyncController。当激活的Controller对象在MvcHandler的BeginProcessRequest方法中是按照这样的方式执行的:如果Controller的类型实现了IAsyncController接口,则调用BeginExecute/EndExecute方法以异步的方式执行Controller;否则Controller的执行通过调用Execute方法以同步方式执行

   1: public interface IController
   2: {    
   3:     void Execute(RequestContext requestContext);
   4: }
   5: public interface IAsyncController : IController
   6: {    
   7:     IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state);
   8:     void EndExecute(IAsyncResult asyncResult);
   9: }

默认情况下通过Visual Studio的向导创建的Controller类型是抽象类型Controller的子类。如下面的代码片断所示,Controller同时实现了IController和IAsyncController这两个接口,所以当MvcHandler进行请求处理时总是以异步的方式来执行Controller。

   1: public abstract class Controller : ControllerBase, IController, IAsyncController, ...
   2: {
   3:     //其他成员
   4:     protected virtual bool DisableAsyncSupport
   5:     {
   6:         get{return false;}
   7:     }
   8: }

但是Controller类型具有一个受保护的只读属性DisableAsyncSupport用于表示是否禁用对异步执行的支持。在默认情况下,该属性值为False,所以默认情况下是支持Controller的异步执行的。如果我们通过重写该属性将值设置为True,那么Controller将只能以同步的方式执行。具体的实现逻辑体现在如下的代码片断中:BeginExecute方法在DisableAsyncSupport属性为True的情况下通过调用Execute方法(该方法会调用一个受保护的虚方法ExecuteCore最终对Controller进行同步执行);否则通过调用BeginExecuteCore/EndExecuteCore以异步方式执行Controller。

   1: public abstract class Controller: ...
   2: {
   3:     //其他成员
   4:     protected virtual IAsyncResult BeginExecute(RequestContext requestContext, 
   5:     AsyncCallback callback, object state)
   6:     {
   7:         if (this.DisableAsyncSupport)
   8:         {
   9:             //通过调用Execute方法同步执行Controller
  10:         }
  11:         else
  12:         {
  13:             //通过调用BeginExecuteCore/EndExecuteCore方法异步执行Controller
  14:         }
  15: }
  16:     protected override void ExecuteCore();
  17:     protected virtual IAsyncResult BeginExecuteCore(AsyncCallback callback, object state);
  18:     protected virtual void EndExecuteCore(IAsyncResult asyncResult);
  19: }

三、 ActionInvoker的同步与异步

包括Model绑定与验证的整个Action的执行通过一个名为ActionInvoker的组件来完成,而它同样具有同步和异步两个版本,分别实现了接口IActionInvoker和IAsyncActionInvoker。如下面的代码片断所示,这两个接口分别通过InvokeAction和BeginInvokeAction/EndInvokeAction方法以同步和异步的方式执行Action。抽象类Controller中具有一个ActionInvoker属性用于设置和返回用于执行自身Action的ActionInvoker对象,而该对象最终是通过受保护需方法CreateActionInvoker创建的。

   1: public interface IActionInvoker
   2: {
   3:     bool InvokeAction(ControllerContext controllerContext, string actionName);
   4: }
   5:  
   6: public interface IAsyncActionInvoker : IActionInvoker
   7: {
   8:     IAsyncResult BeginInvokeAction(ControllerContext controllerContext, string actionName, AsyncCallback callback, object state);
   9:     bool EndInvokeAction(IAsyncResult asyncResult);
  10: }
  11:  
  12: public abstract class Controller
  13: {   
  14:     //其它成员
  15:     public IActionInvoker ActionInvoker { get; set; }
  16:     protected virtual IActionInvoker CreateActionInvoker()
  17: }

ASP.NET MVC真正用于Action方法同步和异步执行的ActionInvoker分别是ControllerActionInvoker和AsyncControllerActionInvoker。如下面的代码片断所示,ControllerActionInvoker定义了一个受保护的方法GetControllerDescriptor用于根据指定的Controller上下文获取相应的ControllerDescriptor,它的子类AsyncControllerActionInvoker对这个方法进行了重写。

   1: public class ControllerActionInvoker : IActionInvoker
   2: {
   3:     //其它成员
   4:     protected virtual ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext);
   5: }
   6:  
   7: public class AsyncControllerActionInvoker : ControllerActionInvoker,IAsyncActionInvoker, IActionInvoker
   8: {
   9:     //其它成员
  10:    protected override ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext);
  11: }

我们所有要了解的是在默认情况下(没有对Controller类型的ActionInvoker属性进行显式设置)采用的ActionInvoker类型是哪个。ASP.NET MVC对Conroller采用的ActionInvoker类型的选择机制是这样的:

  • 通过当前的DependencyResolver以IAsyncActionInvoker接口去获取注册的ActionInvoker,如果返回对象不为Null,则将其作为默认的ActionInvoker。
  • ·通过当前的DependencyResolver以IActionInvoker接口去获取注册的ActionInvoker,如果返回对象不为Null,则将其作为默认的ActionInvoker。
  • 创建AsyncControllerActionInvoker对象作为默认的ActionInvoker。

在默认的情况下,当前的DependencyResolver直接通过对指定的类型进行反射来提供对应的实例对象,所以对于前面两个步骤返回的对象均为Null,所以默认创建出来的ActionInvoker类型为AsyncControllerActionInvoker。我们可以通过如下一个简单的实例来验证这一点。在通过Visual Studio的ASP.NET MVC项目模板创建的空Web应用中,我们创建了如下一个默认的HomeController,在Action方法Index中直接通过ContentResult将ActionInvoker属性的类型名称呈现出来。

   1: public class HomeController : Controller
   2: {  
   3:     public ActionResult Index()
   4:     {
   5:         return Content("默认ActionInvoker类型:" + this.ActionInvoker.GetType().FullName);
   6:     }
   7: }

当运行该Web应用时,会在浏览器上产生如下的输出结果,我们可以清楚地看到默认采用的ActionInvoker类型正是AsyncControllerActionInvoker。

   1: 默认ActionInvoker类型:System.Web.Mvc.Async.AsyncControllerActionInvoker

为了进一步验证基于DependencyResolver对ActionInvoker的提供机制,我们将《ASP.NET MVC Controller激活系统详解:IoC的应用[下篇]》创建的基于Ninject的自定义NinjectDependencyResolver应用在这里。如下面的代码片断所示,在初始化NinjectDependencyResolver的时候,我们将IActionInvoker和IAsyncActionInvoker影射到两个自定义ActionInvoker类型,即FooActionInvoker和FooAsyncActionInvoker,它们分别继承自ControllerActionInvoker和AsyncControllerActionInvoker。

   1: public class NinjectDependencyResolver : IDependencyResolver
   2: {
   3:     public IKernel Kernel { get; private set; }
   4:     public NinjectDependencyResolver()
   5:     {
   6:         this.Kernel = new StandardKernel();
   7:         AddBindings();
   8:     }
   9:     private void AddBindings()
  10:     {
  11:         this.Kernel.Bind<IActionInvoker>().To<FooActionInvoker>();
  12:         this.Kernel.Bind<IAsyncActionInvoker>().To<FooAsyncActionInvoker>();
  13:     }
  14:     public object GetService(Type serviceType)
  15:     {
  16:         return this.Kernel.TryGet(serviceType);
  17:     }
  18:     public IEnumerable<object> GetServices(Type serviceType)
  19:     {
  20:         return this.Kernel.GetAll(serviceType);
  21:     }
  22: }
  23: public class FooActionInvoker : ControllerActionInvoker
  24: {}
  25: public class FooAsyncActionInvoker : AsyncControllerActionInvoker
  26: {}

在Global.asax中对NinjectDependencyResolver进行注册后运行我们的程序,会在浏览器中得到如下的输出结果。IAsyncActionInvoker和FooAsyncActionInvoker进行了影射,NinjectDependencyResolver可以通过IAsyncActionInvoker提供一个FooAsyncActionInvoker实例。

   1: 默认ActionInvoker类型:Artech.Mvc.FooAsyncActionInvoker

现在我们对NinjectDependencyResolver的定义稍加修改,将针对IAsyncActionInvoker接口的类型影射删除,只保留针对IActionInvoker的映射。

   1: public class NinjectDependencyResolver : IDependencyResolver
   2: {
   3:     //其它成员
   4:     private void AddBindings()
   5:     {
   6:         this.Kernel.Bind<IActionInvoker>().To<FooActionInvoker>();
   7:         //this.Kernel.Bind<IAsyncActionInvoker>().To<FooAsyncActionInvoker>();
   8:     }
   9: }

再次运行我们的程序则会得到如下的输出结果。由于NinjectDependencyResolver只能通过IActionInvoker接口提供具体的ActionInvoker,所以最终被创建的是一个FooActionInvoker对象。这个实例演示告诉我们:当我们需要使用到自定义的ActionInvoker的时候,可以通过自定义DependencyResolver以IoC的方式提供具体的ActionInvoker实例。

   1: 默认ActionInvoker类型:Artech.Mvc.FooActionInvoker

 

ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[上篇]
ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[下篇]

posted @ 2012-06-21 09:34  Artech  阅读(12769)  评论(20编辑  收藏  举报