ASP. net mvc executes asynchronous Action

Source: Internet
Author: User

ASP. net mvc check for asynchronous Action

We have formed a Convention: to execute an asynchronous Action, the Controller object must be of the Controller type. The purpose of this Convention is to use the IActionInvoker included in the Controller class -- specifically, it is a function in the ControllerActionInvoker type. Therefore, another convention is that the Controller's ActionInvoker object must return an instance of ControllerActionInvoker.

ControllerActionInvoker has some auxiliary methods that can return a description object for a Controller or Action. We can obtain various information about this Action from an Action description object, and whether it is marked with AsyncActionAttribute is the basis for us to determine whether this Action should be executed asynchronously. As follows:

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

The ControllerActionInvoker type has a protected method GetControllerDescriptor. It accepts a parameter of the ControllerContext type and returns a ControllerDescriptor object to describe the current controller, the description object can obtain an ActionDescriptor object through the FindAction method to describe the Action to be executed. If an Action does not exist, false is returned, and the default Action is executed through the SyncMvcHandler object. If and only when the Action has the AsyncActionAttribute tag, it indicates that it should be executed asynchronously and true is returned. In addition, MethodInvoker is used in this code, which is a helper class derived from Fast Reflection Library. It implements the Reflection call function, but its performance is very similar to direct call of methods, I have described in detail the functions and usage of this project in this article.

This Code involves the improvements made to the ASP. net mvc rc version based on the Beta version. In the original ControllerActionInvoker class, only the MethodInfo of the Action method is obtained, but the abstract types such as the description objects in RC are not. From the current design point of view, we use a subclass of the abstract description type based on reflection. For example, by default, we access ReflectedActionDescriptor type instances through ActionDescriptor abstraction. This is a useful improvement. Since we abstract the description object, we can:

Different implementation methods are used to describe each object. By default, reflection-based or "Convention" is used, if necessary, we can also use the configuration file-based method to replace the existing implementation.

The description of a specific object can be omitted from the internal details. For example, an asynchronous Action may consist of two methods.
With a specific description object, it is also convenient to add additional attributes, such as whether the Action should be executed asynchronously or whether the Session State should be disabled. The above describes how ASP. net mvc executes asynchronous actions.

  1. Introduction to ASP. NET 2.0 Virtual Hosts
  2. Introduction to ASP. NET Applications
  3. Optimize ASP. NET 2.0 Profile Provider
  4. ASP. NET pipeline Optimization
  5. Introduction to ASP. NET Routing Engine

Related Article

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.