自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用ASP.NET MVC源代碼尋找解決方案

開發(fā) 后端
本文介紹用ASP.NET MVC源代碼尋找解決方案,具體實(shí)現(xiàn)非常容易,在這里就展示一下異步Action的編寫方式。

ASP.NET MVC源代碼來尋找解決方案,由于在Action方法中可以調(diào)用BeginXxx方法,我們在AsyncActionResult中只需保留Begin方法返回的IAsyncResult,以及另一個(gè)對于EndXxx方法的引用。在AsyncActionResult的ExecuteResult方法中將會保存這兩個(gè)對象,以便在AsyncMvcHandler的EndProcessRequest方法中重新獲取并使用。根據(jù)“慣例”,我們還需要定義一個(gè)擴(kuò)展方法,方便開發(fā)人員在Action方法中返回一個(gè)AsyncActionResult。具體實(shí)現(xiàn)非常容易,在這里就展示一下異步Action的編寫方式:

  1. [AsyncAction]  
  2. publicActionResultAsyncAction(AsyncCallbackasyncCallback,objectasyncState)  
  3. {  
  4. SqlConnectionconn=newSqlConnection("...;AsynchronousProcessing=true");  
  5. SqlCommandcmd=newSqlCommand("WAITFORDELAY'00:00:03';",conn);  
  6. conn.Open();  
  7.  
  8. returnthis.Async(  
  9. cmd.BeginExecuteNonQuery(asyncCallback,asyncState),  
  10. (ar)=> 
  11. {  
  12. intvalue=cmd.EndExecuteNonQuery(ar);  
  13. conn.Close();  
  14. returnthis.View();  
  15. });  

至此,似乎AsyncMvcHandler也無甚秘密可言了:

  1. publicclassAsyncMvcHandler:IHttpAsyncHandler,IRequiresSessionState  
  2. {  
  3. publicAsyncMvcHandler(  
  4. Controllercontroller,  
  5. IControllerFactorycontrollerFactory,  
  6. RequestContextrequestContext)  
  7. {  
  8. this.Controller=controller;  
  9. this.ControllerFactory=controllerFactory;  
  10. this.RequestContext=requestContext;  
  11. }  
  12.  
  13. publicControllerController{get;privateset;}  
  14. publicRequestContextRequestContext{get;privateset;}  
  15. publicIControllerFactoryControllerFactory{get;privateset;}  
  16. publicHttpContextContext{get;privateset;}  
  17.  
  18. publicIAsyncResultBeginProcessRequest(  
  19. HttpContextcontext,  
  20. AsyncCallbackcb,  
  21. objectextraData)  
  22. {  
  23. this.Context=context;  
  24. this.Controller.SetAsyncCallback(cb).SetAsyncState(extraData);  
  25.  
  26. try  
  27. {  
  28. (this.ControllerasIController).Execute(this.RequestContext);  
  29. returnthis.Controller.GetAsyncResult();  
  30. }  
  31. catch  
  32. {  
  33. this.ControllerFactory.ReleaseController(this.Controller);  
  34. throw;  
  35. }  
  36. }  
  37.  
  38. publicvoidEndProcessRequest(IAsyncResultresult)  
  39. {  
  40. try  
  41. {  
  42. HttpContext.Current=this.Context;  
  43. ActionResultactionResult=this.Controller.GetAsyncEndDelegate()(result);  
  44. if(actionResult!=null)  
  45. {  
  46. actionResult.ExecuteResult(this.Controller.ControllerContext);  
  47. }  
  48. }  
  49. finally  
  50. {  
  51. this.ControllerFactory.ReleaseController(this.Controller);  
  52. }  
  53. }  

在BeginProcessRequest方法中將保存當(dāng)前Context——這點(diǎn)很重要,HttpContext.Current是基于 CallContext的,一旦經(jīng)過一次異步回調(diào)HttpContext.Current就變成了null,我們必須重設(shè)。接著將接收到的 AsyncCallback和AsyncState保留,并使用框架中現(xiàn)成的Execute方法執(zhí)行控制器。當(dāng)Execute方法返回時(shí)一整個(gè)Action方法的調(diào)用流程已經(jīng)結(jié)束,這意味著其調(diào)用結(jié)果——即IAsyncResult和EndDelegate對象已經(jīng)保留。于是將IAsyncResult對象取出并返回。至于EndProcessRequest方法,只是將BeginProcessRequest方法中保存下來的EndDelegate取出,調(diào)用,把得到的ActionResult再執(zhí)行一遍即可。

以上的代碼只涉及到普通情況下的邏輯,而在完整的代碼中還會包括對于Action方法被某個(gè)Filter終止或替換等特殊情況下的處理。此外,無論在BeginProcessRequest還是EndProcessRequest中都需要對異常進(jìn)行合適地處理,使得Controller Factory能夠及時(shí)地對Controller對象進(jìn)行釋放。

如果這個(gè)解決方案沒有缺陷,那么相信它已經(jīng)被放入ASP.NET MVC 1.0中,而輪不到我在這里擴(kuò)展一番了。目前的這個(gè)解決方案至少有以下幾點(diǎn)不足:

沒有嚴(yán)格遵守.NET中的APM模式,雖然不影響功能,但這始終是一個(gè)遺憾。

由于利用了框架中的現(xiàn)成功能,所有的Filter只能運(yùn)行在BeginXxx方法上。

由于EndXxx方法和最終ActionResult的執(zhí)行都沒有Filter支持,因此如果在這個(gè)過程中拋出了異常,將無法進(jìn)入ASP.NET MVC建議的異常處理功能中。

根據(jù)ASP.NET MVC框架的Roadmap,ASP.NET MVC框架1.0之后的版本中將會支持異步Action,相信以上這些缺陷到時(shí)候都能被彌補(bǔ)。不過這就需要大量的工作,這只能交給ASP.NET MVC團(tuán)隊(duì)去慢慢執(zhí)行了。事實(shí)上,您現(xiàn)在已經(jīng)可以在ASP.NET MVC源代碼的MvcFutures項(xiàng)目中找到異步Action處理的相關(guān)內(nèi)容。它添加了 IAsyncController,AsyncController,IAsyncActionInvoker,AsyncControllerActionInvoker 等許多擴(kuò)展。雖說它們都“繼承”了現(xiàn)有的類,但是與我之前的判斷相似,如AsyncControllerActionInvoker幾乎完全重新實(shí)現(xiàn)了一遍ActionInvoker中的各種功能——我還沒有仔細(xì)閱讀代碼,因此無法判斷出這種設(shè)計(jì)是否優(yōu)秀,只希望它能像ASP.NET MVC本身那樣的簡單和優(yōu)雅。

我打算為現(xiàn)在的代碼的EndXxx方法也加上Filter支持,我需要仔細(xì)閱讀ASP.NET MVC源代碼來尋找解決方案。希望它能夠成為ASP.NET MVC正式支持異步Action之前較好的替代方案。

【編輯推薦】

  1. ASP.NET的AsyncState參數(shù)
  2. ASP.NET MVC執(zhí)行異步Action
  3. 概述ASP.NET MVC框架
  4. ASP.NET MVC中使用UpdataModel方法
  5. ASP.NET MVC的Action方法
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2009-04-02 11:00:09

微軟ASP.NETMVC

2012-01-11 10:55:02

ASP.NET MVC

2009-07-24 11:24:33

ASP.NET中文亂碼

2009-07-31 12:43:59

ASP.NET MVC

2009-07-22 17:37:06

ASP.NET Ses

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-23 16:53:17

ASP.NET中文變問

2010-09-02 15:18:42

CSSASP.NET

2009-07-23 15:44:39

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2010-06-04 19:06:47

連接MySQL數(shù)據(jù)庫

2009-07-30 16:02:53

2009-07-29 09:17:12

jQuery刪除

2009-07-23 11:33:18

2009-07-22 10:13:31

異步ActionASP.NET MVC

2009-07-22 09:11:02

Action方法ASP.NET MVC

2009-04-01 12:00:43

ASP.NETMVC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號