日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
ASP.NETMVC涉及到的5個(gè)同步與異步

Action方法的執(zhí)行具有兩種基本的形式,即同步執(zhí)行和異步執(zhí)行,而在ASP.NETMVC的整個(gè)體系中涉及到很多同步/異步的執(zhí)行方式,雖然在前面相應(yīng)的文章中已經(jīng)對(duì)此作了相應(yīng)的介紹,為了讓讀者對(duì)此有一個(gè)整體的了解,我們來做一個(gè)總結(jié)性的論述。

創(chuàng)新互聯(lián)建站專注于企業(yè)營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、寶雞網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為寶雞等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

一、MvcHandler的同步與異步

對(duì)于ASP.NET MVC應(yīng)用來說,MvcHandler是最終用于處理請(qǐng)求的HttpHandler,它是通過UrlRoutingModule這個(gè)實(shí)現(xiàn)了URL路由的HttpModule被動(dòng)態(tài)映射到相應(yīng)的請(qǐng)求的。MvcHandler借助于ControllerFactory激活并執(zhí)行目標(biāo)Controller,并在執(zhí)行結(jié)束后負(fù)責(zé)對(duì)激活的Controller進(jìn)行釋放,相關(guān)的內(nèi)容請(qǐng)參與本書的第3章“Controller的激活”。如下面的代碼片斷所示,MvcHandler同時(shí)實(shí)現(xiàn)了IHttpHandler和IHttpAsyncHandler接口,所以它總是調(diào)用BeginProcessRequest/EndProcessRequest方法以異步的方式來處理請(qǐng)求。

 
 
 
 
  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也具有同步與異步兩個(gè)版本,它們分別實(shí)現(xiàn)了具有如下定義的兩個(gè)接口IController和IAsyncController。當(dāng)激活的Controller對(duì)象在MvcHandler的BeginProcessRequest方法中是按照這樣的方式執(zhí)行的:如果Controller的類型實(shí)現(xiàn)了IAsyncController接口,則調(diào)用BeginExecute/EndExecute方法以異步的方式執(zhí)行Controller;否則Controller的執(zhí)行通過調(diào)用Execute方法以同步方式執(zhí)行。

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

默認(rèn)情況下通過Visual Studio的向?qū)?chuàng)建的Controller類型是抽象類型Controller的子類。如下面的代碼片斷所示,Controller同時(shí)實(shí)現(xiàn)了IController和IAsyncController這兩個(gè)接口,所以當(dāng)MvcHandler進(jìn)行請(qǐng)求處理時(shí)總是以異步的方式來執(zhí)行Controller。

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

但是Controller類型具有一個(gè)受保護(hù)的只讀屬性DisableAsyncSupport用于表示是否禁用對(duì)異步執(zhí)行的支持。在默認(rèn)情況下,該屬性值為False,所以默認(rèn)情況下是支持Controller的異步執(zhí)行的。如果我們通過重寫該屬性將值設(shè)置為True,那么Controller將只能以同步的方式執(zhí)行。具體的實(shí)現(xiàn)邏輯體現(xiàn)在如下的代碼片斷中:BeginExecute方法在DisableAsyncSupport屬性為True的情況下通過調(diào)用Execute方法(該方法會(huì)調(diào)用一個(gè)受保護(hù)的虛方法ExecuteCore最終對(duì)Controller進(jìn)行同步執(zhí)行);否則通過調(diào)用BeginExecuteCore/EndExecuteCore以異步方式執(zhí)行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.              //通過調(diào)用Execute方法同步執(zhí)行Controller  
  10.         }  
  11.         else 
  12.         {  
  13.             //通過調(diào)用BeginExecuteCore/EndExecuteCore方法異步執(zhí)行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綁定與驗(yàn)證的整個(gè)Action的執(zhí)行通過一個(gè)名為ActionInvoker的組件來完成,而它同樣具有同步和異步兩個(gè)版本,分別實(shí)現(xiàn)了接口IActionInvoker和IAsyncActionInvoker。如下面的代碼片斷所示,這兩個(gè)接口分別通過InvokeAction和BeginInvokeAction/EndInvokeAction方法以同步和異步的方式執(zhí)行Action。抽象類Controller中具有一個(gè)ActionInvoker屬性用于設(shè)置和返回用于執(zhí)行自身Action的ActionInvoker對(duì)象,而該對(duì)象最終是通過受保護(hù)需方法CreateActionInvoker創(chuàng)建的。

 
 
 
 
  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方法同步和異步執(zhí)行的ActionInvoker分別是ControllerActionInvoker和AsyncControllerActionInvoker。如下面的代碼片斷所示,ControllerActionInvoker定義了一個(gè)受保護(hù)的方法GetControllerDescriptor用于根據(jù)指定的Controller上下文獲取相應(yīng)的ControllerDescriptor,它的子類AsyncControllerActionInvoker對(duì)這個(gè)方法進(jìn)行了重寫。

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

我們所有要了解的是在默認(rèn)情況下(沒有對(duì)Controller類型的ActionInvoker屬性進(jìn)行顯式設(shè)置)采用的ActionInvoker類型是哪個(gè)。ASP.NET MVC對(duì)Conroller采用的ActionInvoker類型的選擇機(jī)制是這樣的:

通過當(dāng)前的DependencyResolver以IAsyncActionInvoker接口去獲取注冊(cè)的ActionInvoker,如果返回對(duì)象不為Null,則將其作為默認(rèn)的ActionInvoker。

通過當(dāng)前的DependencyResolver以IActionInvoker接口去獲取注冊(cè)的ActionInvoker,如果返回對(duì)象不為Null,則將其作為默認(rèn)的ActionInvoker。

創(chuàng)建AsyncControllerActionInvoker對(duì)象作為默認(rèn)的ActionInvoker。

在默認(rèn)的情況下,當(dāng)前的DependencyResolver直接通過對(duì)指定的類型進(jìn)行反射來提供對(duì)應(yīng)的實(shí)例對(duì)象,所以對(duì)于前面兩個(gè)步驟返回的對(duì)象均為Null,所以默認(rèn)創(chuàng)建出來的ActionInvoker類型為AsyncControllerActionInvoker。我們可以通過如下一個(gè)簡(jiǎn)單的實(shí)例來驗(yàn)證這一點(diǎn)。在通過Visual Studio的ASP.NET MVC項(xiàng)目模板創(chuàng)建的空Web應(yīng)用中,我們創(chuàng)建了如下一個(gè)默認(rèn)的HomeController,在Action方法Index中直接通過ContentResult將ActionInvoker屬性的類型名稱呈現(xiàn)出來。

 
 
 
 
  1.  public class HomeController : Controller  
  2.  {    
  3.      public ActionResult Index()  
  4.      {  
  5.          return Content("默認(rèn)ActionInvoker類型:" + this.ActionInvoker.GetType().FullName);  
  6.      }  

當(dāng)運(yùn)行該Web應(yīng)用時(shí),會(huì)在瀏覽器上產(chǎn)生如下的輸出結(jié)果,我們可以清楚地看到默認(rèn)采用的ActionInvoker類型正是AsyncControllerActionInvoker。

 
 
 
 
  1. 默認(rèn)ActionInvoker類型:System.Web.Mvc.Async.AsyncControllerActionInvoker 

為了進(jìn)一步驗(yàn)證基于DependencyResolver對(duì)ActionInvoker的提供機(jī)制,我們將《ASP.NET MVC Controller激活系統(tǒng)詳解:IoC的應(yīng)用》創(chuàng)建的基于Ninject的自定義NinjectDependencyResolver應(yīng)用在這里。如下面的代碼片斷所示,在初始化NinjectDependencyResolver的時(shí)候,我們將IActionInvoker和IAsyncActionInvoker影射到兩個(gè)自定義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().To();  
  12.         this.Kernel.Bind().To();  
  13.     }  
  14.     public object GetService(Type serviceType)  
  15.     {  
  16.         return this.Kernel.TryGet(serviceType);  
  17.     }  
  18.     public IEnumerable GetServices(Type serviceType)  
  19.     {  
  20.         return this.Kernel.GetAll(serviceType);  
  21.     }  
  22. }  
  23. public class FooActionInvoker : ControllerActionInvoker  
  24. {}  
  25. public class FooAsyncActionInvoker : AsyncControllerActionInvoker  
  26. {} 
  27. 在Global.asax中對(duì)NinjectDependencyResolver進(jìn)行注冊(cè)后運(yùn)行我們的程序,會(huì)在瀏覽器中得到如下的輸出結(jié)果。IAsyncActionInvoker和FooAsyncActionInvoker進(jìn)行了影射,NinjectDependencyResolver可以通過IAsyncActionInvoker提供一個(gè)FooAsyncActionInvoker實(shí)例。

     
     
     
     
    1. 默認(rèn)ActionInvoker類型:Artech.Mvc.FooAsyncActionInvoker 

    現(xiàn)在我們對(duì)NinjectDependencyResolver的定義稍加修改,將針對(duì)IAsyncActionInvoker接口的類型影射刪除,只保留針對(duì)IActionInvoker的映射。

     
     
     
     
    1. public class NinjectDependencyResolver : IDependencyResolver  
    2. {  
    3.     //其它成員  
    4.     private void AddBindings()  
    5.     {  
    6.         this.Kernel.Bind().To();  
    7.         //this.Kernel.Bind().To();  
    8.     }  

    再次運(yùn)行我們的程序則會(huì)得到如下的輸出結(jié)果。由于NinjectDependencyResolver只能通過IActionInvoker接口提供具體的ActionInvoker,所以最終被創(chuàng)建的是一個(gè)FooActionInvoker對(duì)象。這個(gè)實(shí)例演示告訴我們:當(dāng)我們需要使用到自定義的ActionInvoker的時(shí)候,可以通過自定義DependencyResolver以IoC的方式提供具體的ActionInvoker實(shí)例。

     
     
     
     
    1. 默認(rèn)ActionInvoker類型:Artech.Mvc.FooActionInvoker 

    四、ControllerDescriptor的同步與異步

    如果采用ControllerActionInvoker,Action總是以同步的方式來直接,但是當(dāng)AsyncControllerActionInvoker作為Controller的ActionInvoker時(shí),并不意味著總是以異步的方式來執(zhí)行所有的Action。至于這兩種類型的ActionInvoker具體采用對(duì)Action的怎樣的執(zhí)行方式,又涉及到兩個(gè)描述對(duì)象,即用于描述Controller和Action的ControllerDescriptor和ActionDescriptor。

    通過前面“Model的綁定”中對(duì)這兩個(gè)對(duì)象進(jìn)行過相應(yīng)的介紹,我們知道在ASP.NET MVC應(yīng)用編程接口中具有兩個(gè)具體的ControllerDescriptor,即ReflectedControllerDescriptor和ReflectedAsyncControllerDescriptor,它們分別代表同步和異步版本的ControllerDescriptor。

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

    ReflectedControllerDescriptor和ReflectedAsyncControllerDescriptor并非對(duì)分別實(shí)現(xiàn)了IController和IAyncController接口的Controller的描述,而是對(duì)直接繼承自抽象類Controller和AsyncController的Controller的描述。它們之間的區(qū)別在于創(chuàng)建者的不同,在默認(rèn)情況下ReflectedControllerDescriptor和ReflectedAsyncControllerDescriptor分別是通過ControllerActionInvoker和AsyncControllerActionInvoker來創(chuàng)建的。ActionInvoker和ControllerDescriptor之間的關(guān)系可以通過如下圖所示的UML來表示。

    ActionInvoker與ControllerDescriptor之間的關(guān)系可以通過一個(gè)簡(jiǎn)單的實(shí)例來驗(yàn)證。在通過Visual Studio的ASP.NET MVC項(xiàng)目模板創(chuàng)建的空Web應(yīng)用中,我們自定義了如下兩個(gè)分別繼承自ControllerActionInvoker和AsyncControllerActionInvoker的ActionInvoker類型。在這兩個(gè)自定義ActionInvoker中,定義了公有的GetControllerDescriptor方法覆蓋了基類的同名方法(受保護(hù)的虛方法),并直接直接調(diào)用基類的同名方法根據(jù)提供的Controller上下文的到相應(yīng)的ControllerDescriptor對(duì)象。

     
     
     
     
    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.     }  

    然后我們定義了兩個(gè)Controller類型,它們均是抽象類型Controller的直接繼承者。如下面的代碼片斷所示,這兩Controller類(FooController和BarController)都重寫了虛方法CreateActionInvoker,而返回的ActionInvoker類型分別是上面我們定義的FooActionInvoker和BarActionInvoker。在默認(rèn)的Action方法Index中,我們利用當(dāng)前的ActionInvoker得到用于描述本Controller的ControllerDescriptor對(duì)象,并將其類型呈現(xiàn)出來。

     
     
     
     
    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.     }  

    現(xiàn)在我們運(yùn)行我們的程序,并在瀏覽器中輸入相應(yīng)的地址對(duì)定義在FooController和BarController的默認(rèn)Action方法Index發(fā)起訪問,相應(yīng)的ControllerDescriptor類型名稱會(huì)以下圖所示的形式呈現(xiàn)出來,它們的類型分別是ReflectedControllerDescriptorReflectedAsyncControllerDescriptor。

    #p#

    五、ActionDescriptor的執(zhí)行

    Controller包含一組用于描述Action方法的ActionDescriptor對(duì)象。由于Action方法可以采用同步和異步執(zhí)行方法,異步Action對(duì)應(yīng)的ActionDescriptor直接或者間接繼承自抽象類AsyncActionDescriptor,后者是抽象類ActionDescriptor的子類。如下面的代碼片斷所示,同步和異步Action的執(zhí)行分別通過調(diào)用Execute和BeginExecute/EndExecute方法來完成。值得一提的是,AsyncActionDescriptor重寫了Execute方法并直接在此方法中拋出一個(gè)InvalidOperationException異常,也就是說AsyncActionDescriptor對(duì)象只能采用 異步執(zhí)行方式。

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

    通過前面“Model的綁定”我們知道,在ASP.NET MVC應(yīng)用編程接口中采用ReflectedControllerDescriptor來描述同步Action。異步Action方法具有兩種不同的定義方式:其一,通過兩個(gè)匹配的方法XxxAsync/XxxCompleted定義;其二,通過返回類型為Task的方法來定義。這兩種異步Action方法對(duì)應(yīng)的AsyncActionDescriptor類型分別是ReflectedAsyncActionDescriptor和TaskAsyncActionDescriptor。

    對(duì)于ReflectedControllerDescriptor來說,包含其中的ActionDescriptor類型均為ReflectedActionDescriptor。而ReflectedAsyncControllerDescriptor描述的Controller可以同時(shí)包含同步和異步的Action方法,ActionDescriptor的類型取決于Action方法定義的方式。ControllerDescriptor與ActionDescriptor之間的關(guān)系如下圖所示的UML來表示。

    #p#

    實(shí)例演示:AsyncActionInvoker對(duì)ControllerDescriptor的創(chuàng)建

    為了讓讀者對(duì)ActionInvoker對(duì)ControllerDescriptor的解析機(jī)制具有一個(gè)深刻的理解,同時(shí)也作為對(duì)該機(jī)制的驗(yàn)證,我們做一個(gè)簡(jiǎn)單的實(shí)例演示。通過前面的介紹我們知道在默認(rèn)的情況下Controller采用AsyncControllerActionInvoker進(jìn)行Action方法的執(zhí)行,這個(gè)例子就來演示一下它生成的ControllerDescriptor是個(gè)怎樣的對(duì)象。我們通過Visual Studio的ASP.NET MVC項(xiàng)目模板創(chuàng)建一個(gè)空Web應(yīng)用,并創(chuàng)建一個(gè)默認(rèn)的HomeController,然后對(duì)其進(jìn)行如下的修改。

     
     
     
     
    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 + "
      ");  
    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}
      ",actionName,actionDescriptor.GetType().FullName));  
    17.    }  
    18.  
    19.    public void Foo() { }  
    20.    public void BarAsync() { }  
    21.    public void BarCompleted() { }  
    22.    public Task Baz()  
    23.    {  
    24.        throw new NotImplementedException();  
    25.    }      

    我們首先將HomeController的基類從Controller改為AsyncController,并定義了Foo、BarAsync/BarCompleted和Baz四個(gè)方法,我們知道它們對(duì)應(yīng)著Foo、Bar和Baz三個(gè)Action,其中Foo是同步Action,Bar和Baz分別是兩種不同定義形式(XxxAsync/XxxCompleted和Task)的異步Action。

    CheckAction用于根據(jù)指定的Action名稱從ControllerDescriptor對(duì)象中獲取用于表示對(duì)應(yīng)Action的ActionDescriptor對(duì)象,最終將類型名稱呈現(xiàn)出來。在Index方法中,我們通過反射的方式調(diào)用當(dāng)前ActionInvoker(一個(gè)AsyncControllerActionInvoker對(duì)象)的受保護(hù)方法GetControllerDescriptor或者用于描述當(dāng)前Controller(HomeController)的ControllerDescriptor的對(duì)象,并將類型名稱呈現(xiàn)出來。最后通過調(diào)用CheckAction方法將包含在創(chuàng)建的ControllerDescriptor對(duì)象的三個(gè)ActionDescriptor類型呈現(xiàn)出來。

    當(dāng)我們運(yùn)行該程序的時(shí)候,在瀏覽器中會(huì)產(chǎn)生如下的輸出結(jié)果,從中可以看出ControllerDescriptor類型為ReflectedAsyncControllerDescriptor。同步方法Foo對(duì)象的ActionDescriptor是一個(gè)ReflectedActionDescriptor對(duì)象;以XxxAsync/XxxCompleted形式定義的異步方法Bar對(duì)應(yīng)的ActionDescriptor是一個(gè)ReflectedAsyncActionDescriptor對(duì)象;而返回類型為Task的方法Baz對(duì)應(yīng)的ActionDescriptor類型則是TaskAsyncActionDescriptor。

    AsyncController、AsyncControllerActionInvoker與AsyncActionDescriptor

    不論我們采用哪種形式的定義方式,步Action方法都只能定義在繼承自AsyncController的Controller類型中,否則將被認(rèn)為是同步方法。此外,由于通過ControllerActionInvoker只能創(chuàng)建包含ReflectedActionDescriptor的ReflectedControllerDescriptor,如果我們?cè)贏syncController中采用ControllerActionInvoker對(duì)象作為ActionInvoker,所有的Action方法也將被認(rèn)為是同步的。

    我們同樣可以采用一個(gè)簡(jiǎn)單的實(shí)例演示來證實(shí)這一點(diǎn)。在通過Visual Studio的ASP.NET MVC項(xiàng)目模板創(chuàng)建的空Web應(yīng)用中我們定義如下三個(gè)Controller(FooController、BarController和BazController)。我們重寫了它們的CreateActionInvoker方法,返回的ActionInvoker類型(FooActionInvoker和BarActionInvoker)定義在上面,在這里我們將FooActionInvoker和BarActionInvoker看成是ControllerActionInvoker和AsyncControllerActionInvoker(默認(rèn)使用的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 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}
      ", actionDescriptor.ActionName, actionDescriptor.GetType().Name));  
    65.                 }  
    66.             });  
    67.     }  

    我們?cè)谌齻€(gè)Controller中以異步Action的形式定義了兩個(gè)方法DoSomethingAsync和DoSomethingCompleted。FooController繼承自AsyncController,使用AsyncControllerActionInvoker作為其ActionInvoker,這是正常的定義;BarController雖然采用AsyncControllerActionInvoker,但是將抽象類Controller作為其基類;而BazController雖然繼承自ActionInvoker,但ActionInvoker類型為ControllerActionInvoker。在默認(rèn)的Action方法Index中,我們將通過DoSomethingAsync和DoSomethingCompleted方法定義的Action的名稱和對(duì)應(yīng)的ActionDescriptor類型輸出來。

    如果我們運(yùn)行該程序,并在瀏覽器中輸入相應(yīng)的地址對(duì)定義在三個(gè)Controller的默認(rèn)Action方法Index發(fā)起訪問,會(huì)呈現(xiàn)出如下圖所示的結(jié)果。我們可以清楚地看到,對(duì)于以XxxAsync/XxxCompleted形式定義的“異步”Action方法定義,只有針對(duì)AsyncController并且采用AsyncControllerActionInvoker的情況下才會(huì)被解析為一個(gè)異步Action。如果不滿足這兩個(gè)條件,它們會(huì)被視為兩個(gè)普通的同步Action。

    Action方法的執(zhí)行

    目標(biāo)Action方法的最終執(zhí)行由被激活的Controller的ActionInvoker決定,ActionInvoker最終通過調(diào)用對(duì)應(yīng)的ActionDescriptor來執(zhí)行被它描述的Action方法。如果采用ControllerActionInvoker,被它創(chuàng)建的ControllerDescriptor(ReflectedControllerDescriptor)只包含同步的ActionDescriptor(ReflectedActionDescriptor),所以Action方法總是以同步的方式被執(zhí)行。

    如果目標(biāo)Controller是抽象類Controller的直接繼承者,這也是通過Visual Studio的Controller創(chuàng)建向?qū)У哪J(rèn)定義方式,ActionInvoker(ControllerActionInvoker/AsyncControllerActionInvoker)的選擇只決定了創(chuàng)建的ControllerDescriptor的類型(ReflectedControllerDescriptor/ReflectedAsyncControllerDescriptor),ControllerDescriptor包含的所有ActionDescriptor依然是同步的(ReflectedActionDescriptor),所以Action方法也總是以同步的方式被執(zhí)行。

    以異步方式定義的Action方法(XxxAsync/XxxCompleted或采用Task返回類型)只有定義在繼承自AsyncController的Controller類型中,并且采用AsyncControllerActionInvoker作為其ActionInvoker,最終才會(huì)創(chuàng)建AsyncActionDescriptor來描述該Action。也只有同時(shí)滿足這兩個(gè)條件,Action方法才能以異步的方式執(zhí)行。

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

    【編輯推薦】

    1. ASP.NET Cache的一些總結(jié)
    2. ASP.NET Page那點(diǎn)事
    3. 逃脫Asp.Net MVC框架枷鎖 使用Razor視圖引擎
    4. ASP.NET MVC3 從零開始一步步構(gòu)建Web
    5. 用ASP.NET MVC 3實(shí)現(xiàn)一個(gè)訪問統(tǒng)計(jì)系統(tǒng)

    本文題目:ASP.NETMVC涉及到的5個(gè)同步與異步
    文章轉(zhuǎn)載:http://www.5511xx.com/article/djpeesh.html