偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

ASP.NET MVC源代碼來尋找解決方案,由于在Action方法中可以調(diào)用BeginXxx方法,我們在AsyncActionResult中只需保留Begin方法返回的IAsyncResult,以及另一個對于EndXxx方法的引用。在AsyncActionResult的ExecuteResult方法中將會保存這兩個對象,以便在AsyncMvcHandler的EndProcessRequest方法中重新獲取并使用。根據(jù)“慣例”,我們還需要定義一個擴展方法,方便開發(fā)人員在Action方法中返回一個AsyncActionResult。具體實現(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方法中將保存當前Context——這點很重要,HttpContext.Current是基于 CallContext的,一旦經(jīng)過一次異步回調(diào)HttpContext.Current就變成了null,我們必須重設。接著將接收到的 AsyncCallback和AsyncState保留,并使用框架中現(xiàn)成的Execute方法執(zhí)行控制器。當Execute方法返回時一整個Action方法的調(diào)用流程已經(jīng)結束,這意味著其調(diào)用結果——即IAsyncResult和EndDelegate對象已經(jīng)保留。于是將IAsyncResult對象取出并返回。至于EndProcessRequest方法,只是將BeginProcessRequest方法中保存下來的EndDelegate取出,調(diào)用,把得到的ActionResult再執(zhí)行一遍即可。

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

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

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

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

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

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

我打算為現(xiàn)在的代碼的EndXxx方法也加上Filter支持,我需要仔細閱讀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方法
責任編輯:佚名 來源: IT168
相關推薦

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-22 17:37:06

ASP.NET Ses

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2010-09-02 15:18:42

CSSASP.NET

2009-07-23 16:53:17

ASP.NET中文變問

2009-07-20 10:53:59

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-23 15:44:39

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-30 16:02:53

2010-06-04 19:06:47

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

2010-06-23 15:44:03

ASP.NET MVC

2010-03-19 09:17:16

ASP.NET MVC

2009-07-24 11:33:12

MVC單元測試ASP.NET

2009-07-22 10:34:37

ActionInvokASP.NET MVC

2009-07-22 13:08:55

拯救UpdatePanASP.NET MVC
點贊
收藏

51CTO技術棧公眾號