Asynchronous request processing is an advanced feature introduced in ASP. NET 2.0. It relies on the IO Complete Port to improve the throughput of IO-intensive applications. For details, see the principles and performance tests ). However, the ASP. net mvc framework currently lacks the asynchronous Action function, which is the "very important function that ASP. net mvc lacks" that Lao Zhao often talks about ". In the Session of TechED 2008 China, I once gave a so-called "solution", but its high complexity makes that solution too limited.
In order to make up for the regret on TechED and prepare for it. ASP. NET development conference. net mvc best practice Session, I carefully thought about this problem during the Spring Festival vacation, and got a relatively good extension: complete, convenient, it is also very lightweight-the core logic code is only about 200 lines, which means that most of the functions will be delegated to the ready-made content in the framework, ensuring the stability and efficiency of expansion and good backward compatibility.
It is worth mentioning that I am using ASP on the 1/26. the Beta version of net mvc writes the first version of this extension, and Microsoft released ASP soon.. net mvc rc. In the process of porting the solution, I found that ASP. NET MVC RC has made great improvements in the Framework Design, which has led to some changes in my policies when building extensions. Fortunately, these changes in the RC version make it easier to build an extension, especially at the "low-end" level. The ASP. net mvc framework implements its "scalable everywhere" commitment.
Now let's take a closer look at the implementation of this extension.
Change in Request Processing Method
Before formulating basic transformation strategies, we need to understand the current architecture and request processing process of ASP. net mvc framework. As follows:
When the application is started, no request is received.) register the Route policy for the MVC request to the ASP. NET Routing module. In this case, the RouteHandler attribute in each Route policy is the MvcRouteHandler in ASP. net mvc framework.
When ASP. when the NET Routing module receives an HTTP request that matches a specific Route policy, it will call the GetHttpHandler of the RouteHandler object in the Route object to obtain an HttpHandler and submit it to ASP.. NET. MvcRouteHandler will always return an MvcHandler object.
MvcHandler extracts the controller value in RouteData during execution, constructs a controller object that implements the IController interface, and calls the Execute method of the IController interface to Execute the controller.
For an ASP. net mvc application, most controllers will inherit the System. Web. Mvc. Controller type. The Controller class will get the action value from RouteData and hand it to the object implementing the IActionInvoker interface to execute an Action.
......
If we want to transform this process into asynchronous processing, we need to make it conform to the asynchronous Processing Method in ASP. NET architecture. The ASP. NET architecture can process asynchronous requests in several ways, such as Asynchronous pages and asynchronous Http Module. asynchronous Http Handler is the most suitable method for the current situation. To implement an asynchronous Handler, we need to implement the IHttpAsyncHandler interface for the Handler that processes the request, instead of the traditional IHttpHandler interface. The BeginProcessRequest and EndProcessRequest methods in the IHttpAsyncHandler interface constitute the APMAynchronous Programming Model and asynchronous Programming Model in. NET. You can use the "two-stage" Asynchronous call to process an HTTP request.
You should have discovered that to support asynchronous Action, you must confirm whether to execute an IHttpHandler object or IHttpAsyncHandler object based on the current request information. By default, the ASP. net mvc Framework checks, constructs, and CALLS controllers within Http Handler (MvcHandler object. This is too late. We must talk about these logics before the Routing process. Fortunately, the IRouteHandler supported by ASP. NET Routing is like IHttpHandlerFactory in ASP. NET. Different Handler can be generated as needed for execution. Therefore, we only need to build a new IRouteHandler type. So AsyncMvcRouteHandler was born. As you can imagine, some of the Code is the same as MvcHandler in the framework, to some extent, we did just advance what was originally done in MvcHandler:
- publicclassAsyncMvcRouteHandler:IRouteHandler
- {
- publicIHttpHandlerGetHttpHandler(RequestContextrequestContext)
- {
- stringcontrollerName=requestContext.RouteData.GetRequiredString("controller");
-
- varfactory=ControllerBuilder.Current.GetControllerFactory();
- varcontroller=factory.CreateController(requestContext,controllerName);
- if(controller==null)
- {
- thrownewInvalidOperationException(...);
- }
-
- varcoreController=controllerasController;
- if(coreController==null)
- {
- returnnewSyncMvcHandler(controller,factory,requestContext);
- }
- else
- {
-
- stringactionName=requestContext.RouteData.GetRequiredString("action");
- returnIsAsyncAction(coreController,actionName,requestContext)?
- (IHttpHandler)newAsyncMvcHandler(coreController,factory,requestContext):
- (IHttpHandler)newSyncMvcHandler(controller,factory,requestContext);
- }
- }
-
- internalstaticboolIsAsyncAction(
- Controllercontroller,stringactionName,RequestContextrequestContext)
- {
- ...
- }
- }
- 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