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

2013 年 11 月 1 日5020

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

  一、MvcHandler的同步与异步

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

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

  二、Controller的同步与异步

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

  public interface IController { void Execute(RequestContext requestContext); } public interface IAsyncController : IController { IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state); void EndExecute(IAsyncResult asyncResult); }

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

  public abstract class Controller : ControllerBase, IController, IAsyncController, ... { //其他成员 protected virtual bool DisableAsyncSupport { get{return false;} } }

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

  public abstract class Controller: ... { //其他成员 protected virtual IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state) { if (this.DisableAsyncSupport) { //通过调用Execute方法同步执行Controller } else { //通过调用BeginExecuteCore/EndExecuteCore方法异步执行Controller } } protected override void ExecuteCore(); protected virtual IAsyncResult BeginExecuteCore(AsyncCallback callback, object state); protected virtual void EndExecuteCore(IAsyncResult asyncResult); }

  三、 ActionInvoker的同步与异步

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

  public interface IActionInvoker { bool InvokeAction(ControllerContext controllerContext, string actionName); } public interface IAsyncActionInvoker : IActionInvoker { IAsyncResult BeginInvokeAction(ControllerContext controllerContext, string actionName, AsyncCallback callback, object state); bool EndInvokeAction(IAsyncResult asyncResult); } public abstract class Controller { //其它成员 public IActionInvoker ActionInvoker { get; set; } protected virtual IActionInvoker CreateActionInvoker() }

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

  public class ControllerActionInvoker : IActionInvoker { //其它成员 protected virtual ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext); } public class AsyncControllerActionInvoker : ControllerActionInvoker,IAsyncActionInvoker, IActionInvoker { //其它成员 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属性的类型名称呈现出来。

  public class HomeController : Controller { public ActionResult Index() { return Content("默认ActionInvoker类型:" + this.ActionInvoker.GetType().FullName); } }

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

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

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

  public class NinjectDependencyResolver : IDependencyResolver { public IKernel Kernel { get; private set; } public NinjectDependencyResolver() { this.Kernel = new StandardKernel(); AddBindings(); } private void AddBindings() { this.Kernel.Bind().To(); this.Kernel.Bind().To(); } public object GetService(Type serviceType) { return this.Kernel.TryGet(serviceType); } public IEnumerable

0 0