For SyncMvcHandler that executes synchronous actions, its implementation is very simple and direct. The following is the ActionInvoker of ASP. net mvc framework.
- 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);
- }
- }
- }
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?
- publicclassAsyncActionResult:ActionResult
- {
- publicAsyncActionResult(
- IAsyncResultasyncResult,
- Func<IAsyncResult,ActionResult>endDelegate)
- {
- this.AsyncResult=asyncResult;
- this.EndDelegate=endDelegate;
- }
-
- publicIAsyncResultAsyncResult{get;privateset;}
-
- publicFunc<IAsyncResult,ActionResult>EndDelegate{get;privateset;}
-
- publicoverridevoidExecuteResult(ControllerContextcontext)
- {
- context.Controller
- .SetAsyncResult(this.AsyncResult)
- .SetAsyncEndDelegate(this.EndDelegate);
- }
- }
- AsyncState parameter of ASP. NET
- ASP. net mvc executes asynchronous Action
- Overview ASP. net mvc Framework
- Use the UpdataModel method in ASP. NET MVC
- ASP. net mvc Action Method