ASP.NET MVC涉及到的5个同步与异步

开发 后端
在ASP.NETMVC的整个体系中涉及到很多同步/异步的执行方式,虽然在前面相应的文章中已经对此作了相应的介绍,为了让读者对此有一个整体的了解,我们来做一个总结性的论述。

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

一、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);  

#p#

二、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);  

默认情况下通过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.     }  

但是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);  

#p#

三、 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()  

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);  

我们所有要了解的是在默认情况下(没有对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.      }  

当运行该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.     }  

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

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

四、ControllerDescriptor的同步与异步

如果采用ControllerActionInvoker,Action总是以同步的方式来直接,但是当AsyncControllerActionInvoker作为Controller的ActionInvoker时,并不意味着总是以异步的方式来执行所有的Action。至于这两种类型的ActionInvoker具体采用对Action的怎样的执行方式,又涉及到两个描述对象,即用于描述Controller和Action的ControllerDescriptor和ActionDescriptor。

通过前面“Model的绑定”中对这两个对象进行过相应的介绍,我们知道在ASP.NET MVC应用编程接口中具有两个具体的ControllerDescriptor,即ReflectedControllerDescriptor和ReflectedAsyncControllerDescriptor,它们分别代表同步和异步版本的ControllerDescriptor。

  1. public class ReflectedControllerDescriptor : ControllerDescriptor  
  2. {  
  3.     //省略成员  
  4. }  
  5.    
  6. public class ReflectedAsyncControllerDescriptor : ControllerDescriptor  
  7. {  
  8.     //省略成员  

ReflectedControllerDescriptor和ReflectedAsyncControllerDescriptor并非对分别实现了IController和IAyncController接口的Controller的描述,而是对直接继承自抽象类Controller和AsyncController的Controller的描述。它们之间的区别在于创建者的不同,在默认情况下ReflectedControllerDescriptor和ReflectedAsyncControllerDescriptor分别是通过ControllerActionInvoker和AsyncControllerActionInvoker来创建的。ActionInvoker和ControllerDescriptor之间的关系可以通过如下图所示的UML来表示。

image

ActionInvoker与ControllerDescriptor之间的关系可以通过一个简单的实例来验证。在通过Visual Studio的ASP.NET MVC项目模板创建的空Web应用中,我们自定义了如下两个分别继承自ControllerActionInvoker和AsyncControllerActionInvoker的ActionInvoker类型。在这两个自定义ActionInvoker中,定义了公有的GetControllerDescriptor方法覆盖了基类的同名方法(受保护的虚方法),并直接直接调用基类的同名方法根据提供的Controller上下文的到相应的ControllerDescriptor对象。

  1.  public class FooActionInvoker : ControllerActionInvoker  
  2.  {   
  3.      public new ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext)  
  4.      {  
  5.          return base.GetControllerDescriptor(controllerContext);  
  6.      }  
  7.  }  
  8.     
  9.  public class BarActionInvoker : AsyncControllerActionInvoker  
  10. {  
  11.     public new ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext)  
  12.     {  
  13.         return base.GetControllerDescriptor(controllerContext);  
  14.     }  

然后我们定义了两个Controller类型,它们均是抽象类型Controller的直接继承者。如下面的代码片断所示,这两Controller类(FooController和BarController)都重写了虚方法CreateActionInvoker,而返回的ActionInvoker类型分别是上面我们定义的FooActionInvoker和BarActionInvoker。在默认的Action方法Index中,我们利用当前的ActionInvoker得到用于描述本Controller的ControllerDescriptor对象,并将其类型呈现出来。

  1.  public class FooController : Controller  
  2.  {  
  3.      protected override IActionInvoker CreateActionInvoker()  
  4.      {  
  5.          return new FooActionInvoker();  
  6.      }  
  7.     
  8.      public void Index()  
  9.      {  
  10.         ControllerDescriptor controllerDescriptor = ((FooActionInvoker)this.ActionInvoker).GetControllerDescriptor(ControllerContext);  
  11.         Response.Write(controllerDescriptor.GetType().FullName);  
  12.     }  
  13. }  
  14.    
  15. public class BarController : Controller  
  16. {  
  17.     protected override IActionInvoker CreateActionInvoker()  
  18.     {  
  19.         return new BarActionInvoker();  
  20.     }  
  21.    
  22.     public void Index()  
  23.     {  
  24.         ControllerDescriptor controllerDescriptor = ((BarActionInvoker)this.ActionInvoker).GetControllerDescriptor(ControllerContext);  
  25.         Response.Write(controllerDescriptor.GetType().FullName);  
  26.     }  

现在我们运行我们的程序,并在浏览器中输入相应的地址对定义在FooController和BarController的默认Action方法Index发起访问,相应的ControllerDescriptor类型名称会以下图所示的形式呈现出来,它们的类型分别是ReflectedControllerDescriptorReflectedAsyncControllerDescriptor

image

#p#

五、ActionDescriptor的执行

Controller包含一组用于描述Action方法的ActionDescriptor对象。由于Action方法可以采用同步和异步执行方法,异步Action对应的ActionDescriptor直接或者间接继承自抽象类AsyncActionDescriptor,后者是抽象类ActionDescriptor的子类。如下面的代码片断所示,同步和异步Action的执行分别通过调用Execute和BeginExecute/EndExecute方法来完成。值得一提的是,AsyncActionDescriptor重写了Execute方法并直接在此方法中抛出一个InvalidOperationException异常,也就是说AsyncActionDescriptor对象只能采用 异步执行方式。

  1.  public abstract class ActionDescriptor : ICustomAttributeProvider  
  2.  {  
  3.      //其他成员  
  4.      public abstract object Execute(ControllerContext controllerContext, IDictionary<string, object> parameters);  
  5.  }  
  6.     
  7.  public abstract class AsyncActionDescriptor : ActionDescriptor  
  8.  {  
  9.      //其他成员  
  10.     public abstract IAsyncResult BeginExecute(ControllerContext controllerContext, IDictionary<string, object> parameters, AsyncCallback callback, object state);  
  11.     public abstract object EndExecute(IAsyncResult asyncResult);  

通过前面“Model的绑定”我们知道,在ASP.NET MVC应用编程接口中采用ReflectedControllerDescriptor来描述同步Action。异步Action方法具有两种不同的定义方式:其一,通过两个匹配的方法XxxAsync/XxxCompleted定义;其二,通过返回类型为Task的方法来定义。这两种异步Action方法对应的AsyncActionDescriptor类型分别是ReflectedAsyncActionDescriptor和TaskAsyncActionDescriptor

对于ReflectedControllerDescriptor来说,包含其中的ActionDescriptor类型均为ReflectedActionDescriptor。而ReflectedAsyncControllerDescriptor描述的Controller可以同时包含同步和异步的Action方法,ActionDescriptor的类型取决于Action方法定义的方式。ControllerDescriptor与ActionDescriptor之间的关系如下图所示的UML来表示。

image

#p#

实例演示:AsyncActionInvoker对ControllerDescriptor的创建

为了让读者对ActionInvoker对ControllerDescriptor的解析机制具有一个深刻的理解,同时也作为对该机制的验证,我们做一个简单的实例演示。通过前面的介绍我们知道在默认的情况下Controller采用AsyncControllerActionInvoker进行Action方法的执行,这个例子就来演示一下它生成的ControllerDescriptor是个怎样的对象。我们通过Visual Studio的ASP.NET MVC项目模板创建一个空Web应用,并创建一个默认的HomeController,然后对其进行如下的修改。

  1. public class HomeController : AsyncController  
  2. {  
  3.     public void Index()  
  4.     {  
  5.         MethodInfo method = typeof(AsyncControllerActionInvoker).GetMethod("GetControllerDescriptor", BindingFlags.Instance | BindingFlags.NonPublic);  
  6.         ControllerDescriptor controllerDescriptor = (ControllerDescriptor)method.Invoke(this.ActionInvoker, new object[] { this.ControllerContext });  
  7.         Response.Write(controllerDescriptor.GetType().FullName + "<br/>");  
  8.         CheckAction(controllerDescriptor, "Foo");  
  9.         CheckAction(controllerDescriptor, "Bar");  
  10.        CheckAction(controllerDescriptor, "Baz");  
  11.  
  12.    }  
  13.     private void CheckAction(ControllerDescriptor controllerDescriptor,string actionName)  
  14.    {  
  15.        ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(this.ControllerContext, actionName);  
  16.        Response.Write(string.Format("{0}: {1}<br/>",actionName,actionDescriptor.GetType().FullName));  
  17.    }  
  18.  
  19.    public void Foo() { }  
  20.    public void BarAsync() { }  
  21.    public void BarCompleted() { }  
  22.    public Task<ActionResult> Baz()  
  23.    {  
  24.        throw new NotImplementedException();  
  25.    }      

我们首先将HomeController的基类从Controller改为AsyncController,并定义了Foo、BarAsync/BarCompleted和Baz四个方法,我们知道它们对应着Foo、Bar和Baz三个Action,其中Foo是同步Action,Bar和Baz分别是两种不同定义形式(XxxAsync/XxxCompleted和Task)的异步Action。

CheckAction用于根据指定的Action名称从ControllerDescriptor对象中获取用于表示对应Action的ActionDescriptor对象,最终将类型名称呈现出来。在Index方法中,我们通过反射的方式调用当前ActionInvoker(一个AsyncControllerActionInvoker对象)的受保护方法GetControllerDescriptor或者用于描述当前Controller(HomeController)的ControllerDescriptor的对象,并将类型名称呈现出来。最后通过调用CheckAction方法将包含在创建的ControllerDescriptor对象的三个ActionDescriptor类型呈现出来。

当我们运行该程序的时候,在浏览器中会产生如下的输出结果,从中可以看出ControllerDescriptor类型为ReflectedAsyncControllerDescriptor。同步方法Foo对象的ActionDescriptor是一个ReflectedActionDescriptor对象;以XxxAsync/XxxCompleted形式定义的异步方法Bar对应的ActionDescriptor是一个ReflectedAsyncActionDescriptor对象;而返回类型为Task的方法Baz对应的ActionDescriptor类型则是TaskAsyncActionDescriptor。

image

AsyncController、AsyncControllerActionInvoker与AsyncActionDescriptor

不论我们采用哪种形式的定义方式,步Action方法都只能定义在继承自AsyncController的Controller类型中,否则将被认为是同步方法。此外,由于通过ControllerActionInvoker只能创建包含ReflectedActionDescriptor的ReflectedControllerDescriptor,如果我们在AsyncController中采用ControllerActionInvoker对象作为ActionInvoker,所有的Action方法也将被认为是同步的。

我们同样可以采用一个简单的实例演示来证实这一点。在通过Visual Studio的ASP.NET MVC项目模板创建的空Web应用中我们定义如下三个Controller(FooController、BarController和BazController)。我们重写了它们的CreateActionInvoker方法,返回的ActionInvoker类型(FooActionInvoker和BarActionInvoker)定义在上面,在这里我们将FooActionInvoker和BarActionInvoker看成是ControllerActionInvoker和AsyncControllerActionInvoker(默认使用的ActionInvoker)。

  1.  public class FooController : AsyncController  
  2.  {  
  3.      protected override IActionInvoker CreateActionInvoker()  
  4.      {  
  5.          return new BarActionInvoker();  
  6.      }  
  7.      public void Index()  
  8.      {  
  9.          BarActionInvoker actionInvoker = (BarActionInvoker)this.ActionInvoker;  
  10.         Utility.DisplayActions(controllerContext=>actionInvoker.GetControllerDescriptor(ControllerContext),ControllerContext);  
  11.     }  
  12.    
  13.     public void DoSomethingAsync()  
  14.     { }  
  15.     public void DoSomethingCompleted()  
  16.     { }  
  17. }  
  18.    
  19. public class BarController : Controller  
  20. {  
  21.     protected override IActionInvoker CreateActionInvoker()  
  22.     {  
  23.         return new BarActionInvoker();  
  24.     }  
  25.     public void Index()  
  26.     {  
  27.         BarActionInvoker actionInvoker = (BarActionInvoker)this.ActionInvoker;  
  28.         Utility.DisplayActions(controllerContext => actionInvoker.GetControllerDescriptor(ControllerContext), ControllerContext);  
  29.     }  
  30.     public void DoSomethingAsync()  
  31.     { }  
  32.     public void DoSomethingCompleted()  
  33.     { }  
  34. }  
  35.    
  36. public class BazController : Controller  
  37. {  
  38.     protected override IActionInvoker CreateActionInvoker()  
  39.     {  
  40.         return new FooActionInvoker();  
  41.     }  
  42.     public void Index()  
  43.     {  
  44.         FooActionInvoker actionInvoker = (FooActionInvoker)this.ActionInvoker;  
  45.         Utility.DisplayActions(controllerContext => actionInvoker.GetControllerDescriptor(ControllerContext), ControllerContext);  
  46.     }  
  47.     public void DoSomethingAsync()  
  48.     { }  
  49.     public void DoSomethingCompleted()  
  50.     { }  
  51. }  
  52.    
  53. public static class Utility  
  54. {  
  55.     public static void DisplayActions(Func<ControllerContext, ControllerDescriptor> controllerDescriptorAccessor, ControllerContext controllerContext)  
  56.     {   
  57.         ControllerDescriptor controllerDescriptor = controllerDescriptorAccessor(controllerContext);  
  58.         string[] actionNames = { "DoSomething""DoSomethingAsync""DoSomethingCompleted" };  
  59.         Array.ForEach(actionNames, actionName =>  
  60.             {  
  61.                 ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(controllerContext,actionName);  
  62.                 if (null != actionDescriptor)  
  63.                 {  
  64.                     HttpContext.Current.Response.Write(string.Format("{0}: {1}<br/>", actionDescriptor.ActionName, actionDescriptor.GetType().Name));  
  65.                 }  
  66.             });  
  67.     }  

我们在三个Controller中以异步Action的形式定义了两个方法DoSomethingAsync和DoSomethingCompleted。FooController继承自AsyncController,使用AsyncControllerActionInvoker作为其ActionInvoker,这是正常的定义;BarController虽然采用AsyncControllerActionInvoker,但是将抽象类Controller作为其基类;而BazController虽然继承自ActionInvoker,但ActionInvoker类型为ControllerActionInvoker。在默认的Action方法Index中,我们将通过DoSomethingAsync和DoSomethingCompleted方法定义的Action的名称和对应的ActionDescriptor类型输出来。

如果我们运行该程序,并在浏览器中输入相应的地址对定义在三个Controller的默认Action方法Index发起访问,会呈现出如下图所示的结果。我们可以清楚地看到,对于以XxxAsync/XxxCompleted形式定义的“异步”Action方法定义,只有针对AsyncController并且采用AsyncControllerActionInvoker的情况下才会被解析为一个异步Action。如果不满足这两个条件,它们会被视为两个普通的同步Action。

image

Action方法的执行

目标Action方法的最终执行由被激活的Controller的ActionInvoker决定,ActionInvoker最终通过调用对应的ActionDescriptor来执行被它描述的Action方法。如果采用ControllerActionInvoker,被它创建的ControllerDescriptor(ReflectedControllerDescriptor)只包含同步的ActionDescriptor(ReflectedActionDescriptor),所以Action方法总是以同步的方式被执行。

如果目标Controller是抽象类Controller的直接继承者,这也是通过Visual Studio的Controller创建向导的默认定义方式,ActionInvoker(ControllerActionInvoker/AsyncControllerActionInvoker)的选择只决定了创建的ControllerDescriptor的类型(ReflectedControllerDescriptor/ReflectedAsyncControllerDescriptor),ControllerDescriptor包含的所有ActionDescriptor依然是同步的(ReflectedActionDescriptor),所以Action方法也总是以同步的方式被执行。

以异步方式定义的Action方法(XxxAsync/XxxCompleted或采用Task返回类型)只有定义在继承自AsyncController的Controller类型中,并且采用AsyncControllerActionInvoker作为其ActionInvoker,最终才会创建AsyncActionDescriptor来描述该Action。也只有同时满足这两个条件,Action方法才能以异步的方式执行。

原文链接:http://www.cnblogs.com/artech/archive/2012/06/22/sync-and-async-02.html

【编辑推荐】

  1. ASP.NET Cache的一些总结
  2. ASP.NET Page那点事
  3. 逃脱Asp.Net MVC框架枷锁 使用Razor视图引擎
  4. ASP.NET MVC3 从零开始一步步构建Web
  5. 用ASP.NET MVC 3实现一个访问统计系统
责任编辑:张伟 来源: Artech的博客
相关推荐

2009-07-22 10:13:31

异步ActionASP.NET MVC

2015-07-03 10:12:04

编程同步非阻塞

2009-03-06 10:28:30

MVCASP.NET异步Action

2012-02-03 09:06:53

ASP.NET MVC

2009-02-16 10:05:11

ActionMVCASP.NET

2009-02-17 09:22:14

ActionMVCASP.NET

2009-07-24 13:20:44

MVC框架ASP.NET

2009-12-08 08:57:21

ASP.NET MVC

2009-07-31 12:43:59

ASP.NET MVC

2009-03-13 10:58:48

ASP.NetMVC框架编程

2009-08-24 09:18:34

ASP.NET MVC

2015-06-29 10:00:02

ASP.NETMVC6

2014-08-26 09:22:40

ASP.NET MVCRouting

2010-02-05 08:32:32

ASP.NET MVC

2009-07-22 10:34:37

ActionInvokASP.NET MVC

2009-07-20 15:44:32

ASP.NET MVC

2009-07-22 09:11:02

Action方法ASP.NET MVC

2012-03-31 10:59:02

ASP.NET

2009-08-18 17:50:37

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC
点赞
收藏

51CTO技术栈公众号