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:
- publicclassSyncMvcHandler:IHttpHandler,IRequiresSessionState
- {
- publicSyncMvcHandler(
- IControllercontroller,
- IControllerFactorycontrollerFactory,
- RequestContextrequestContext)
- {
- this.Controller=controller;
- this.ControllerFactory=controllerFactory;
- this.RequestContext=requestContext;
- }
-
- publicIControllerController{get;privateset;}
- publicRequestContextRequestContext{get;privateset;}
- publicIControllerFactoryControllerFactory{get;privateset;}
-
- publicvirtualboolIsReusable{get{returnfalse;}}
-
- publicvirtualvoidProcessRequest(HttpContextcontext)
- {
- try
- {
- this.Controller.Execute(this.RequestContext);
- }
- finally
- {
- this.ControllerFactory.ReleaseController(this.Controller);
- }
- }
- }
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.
- Introduction to ASP. NET 2.0 Virtual Hosts
- Introduction to ASP. NET Applications
- Optimize ASP. NET 2.0 Profile Provider
- ASP. NET pipeline Optimization
- Introduction to ASP. NET Routing Engine