ActionInvoker of ASP. net mvc Framework

Source: Internet
Author: User

For SyncMvcHandler that executes synchronous actions, its implementation is very simple and direct. The following is the ActionInvoker of ASP. net mvc framework.

 
 
  1. publicclassSyncMvcHandler:IHttpHandler,IRequiresSessionState  
  2. {  
  3. publicSyncMvcHandler(  
  4. IControllercontroller,  
  5. IControllerFactorycontrollerFactory,  
  6. RequestContextrequestContext)  
  7. {  
  8. this.Controller=controller;  
  9. this.ControllerFactory=controllerFactory;  
  10. this.RequestContext=requestContext;  
  11. }  
  12.  
  13. publicIControllerController{get;privateset;}  
  14. publicRequestContextRequestContext{get;privateset;}  
  15. publicIControllerFactoryControllerFactory{get;privateset;}  
  16.  
  17. publicvirtualboolIsReusable{get{returnfalse;}}  
  18.  
  19. publicvirtualvoidProcessRequest(HttpContextcontext)  
  20. {  
  21. try  
  22. {  
  23. this.Controller.Execute(this.RequestContext);  
  24. }  
  25. finally  
  26. {  
  27. this.ControllerFactory.ReleaseController(this.Controller);  
  28. }  
  29. }  

For Asynchronous actions, I have been thinking about how to convert the default implementation of the framework, that is, a single method call, into two methods BeginXxx/EndXxx) Call. I once thought about implementing a new ActionInvoker myself, but this involves a lot of work, especially if you want to maintain ASP. the existing functions of the net mvc framework include ActionFilter and ActionSelector. The most effort-saving method may be to inherit ControllerActionInvoker and try to use various auxiliary methods implemented by the framework. However, after analyzing the Framework Code, I found that reuse is also very difficult. For example, one of the bases for ControllerActionInvoker to determine a method as Action is that this method returns the ActionResult type or its subclass, this means that I cannot directly use this method to obtain a BeginXxx method that returns IAsyncResult. Similarly, when I request an asynchronous Action named Abc, use EndAbc as the search basis and submit it to a ready-made method for query -- but what if another request is directly directed to a synchronous Action named EndAbc?

As a result of these problems, I tried to Implement Asynchronous actions last year and almost overwritten the entire ActionInvoker-the complexity is evident. In addition, the implementation is still unfriendly to some special situations and requires developers to make compromises to some extent. This implementation was announced in the Session of TechED 2008 China, and I admit that it is not satisfactory to me. I suggest you do not put it into the production environment. The current implementation has successfully solved the entire problem. Although theoretically not "perfect", some concessions have been made.

The reason for so many problems is that we are trying to subvert the key design within the framework, that is, switching from a single Action method call to an APM-compliant two-stage call. Are you aware of the key to solving the problem? That's right. It's "compatible with APM ". APM requires us to divide a behavior into BeginXxx and EndXxx methods. However, since the ASP. net mvc framework only allows us to return an ActionResult object ...... So why isn't this object containing reference of a method-that is, a delegate object? Although this is not in line with the Orthodox APM signature, it is completely feasible, isn't it?

 
 
  1. publicclassAsyncActionResult:ActionResult  
  2. {  
  3. publicAsyncActionResult(  
  4. IAsyncResultasyncResult,  
  5. Func<IAsyncResult,ActionResult>endDelegate)  
  6. {  
  7. this.AsyncResult=asyncResult;  
  8. this.EndDelegate=endDelegate;  
  9. }  
  10.  
  11. publicIAsyncResultAsyncResult{get;privateset;}  
  12.  
  13. publicFunc<IAsyncResult,ActionResult>EndDelegate{get;privateset;}  
  14.  
  15. publicoverridevoidExecuteResult(ControllerContextcontext)  
  16. {  
  17. context.Controller  
  18. .SetAsyncResult(this.AsyncResult)  
  19. .SetAsyncEndDelegate(this.EndDelegate);  
  20. }  
  1. AsyncState parameter of ASP. NET
  2. ASP. net mvc executes asynchronous Action
  3. Overview ASP. net mvc Framework
  4. Use the UpdataModel method in ASP. NET MVC
  5. ASP. net mvc Action Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.