日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

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

發(fā)布時(shí)間:2025/4/5 asp.net 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[下篇] 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

關(guān)于ASP.NET MVC對(duì)請(qǐng)求的處理方式(同步或者異步)涉及到的五個(gè)組件,在《上篇》中我們談了三個(gè)(MvcHandler、Controller和ActionInvoker),現(xiàn)在我們來談?dòng)嘞碌膬蓚€(gè),即ControllerDescriptor和ActionDescriptor,這五個(gè)組件的執(zhí)行并非孤立的,而是具有議定的關(guān)系。相信讀者認(rèn)真閱讀了這兩篇文章后,會(huì)對(duì)整個(gè)請(qǐng)求的處理方式有一個(gè)深刻的理解。[本文已經(jīng)同步到《How ASP.NET MVC Works?》中]

目錄
一、MvcHandler的同步于異步
二、Controller的同步與異步
三、ActionInvoker的同步與異步
四、ControllerDescriptor的同步與異步
五、ActionDescriptor的同步與異步
??? 1、實(shí)例演示:AsyncActionInvoker對(duì)ControllerDescriptor的創(chuàng)建
??? 2、AsyncController、AsyncControllerActionInvoker與AsyncActionDescriptor
??? 3、Action方法的執(zhí)行

四、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: //省略成員 9: }

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è)簡單的實(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: } 15: }

然后我們定義了兩個(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: } 27: }

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

五、ActionDescriptor的執(zhí)行

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

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); 12: }

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

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

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

為了讓讀者對(duì)ActionInvoker對(duì)ControllerDescriptor的解析機(jī)制具有一個(gè)深刻的理解,同時(shí)也作為對(duì)該機(jī)制的驗(yàn)證,我們做一個(gè)簡單的實(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 + "<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: } 26: }

我們首先將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è)簡單的實(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<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: } 68: }

我們?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í)行。

ASP.NET MVC涉及到的5個(gè)同步與異步,你是否傻傻分不清楚?[上篇]
ASP.NET MVC涉及到的5個(gè)同步與異步,你是否傻傻分不清楚?[下篇]

轉(zhuǎn)載于:https://www.cnblogs.com/artech/archive/2012/06/22/sync-and-async-02.html

總結(jié)

以上是生活随笔為你收集整理的ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[下篇]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。