Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find a solution with ASP.NET MVC source code

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to use ASP.NET MVC source code to find a solution, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

ASP.NET MVC source code to find a solution, since the BeginXxx method can be called in the Action method, we only need to keep the IAsyncResult returned by the Begin method and another reference to the EndXxx method in AsyncActionResult. These two objects are saved in the ExecuteResult method of AsyncActionResult so that they can be retrieved and used in the EndProcessRequest method of AsyncMvcHandler. According to the convention, we also need to define an extension method to make it easy for the developer to return an AsyncActionResult in the Action method. It's very easy to implement. Here's how to write an asynchronous Action:

[AsyncAction] publicActionResultAsyncAction (AsyncCallbackasyncCallback,objectasyncState) {SqlConnectionconn=newSqlConnection ("...; AsynchronousProcessing=true"); SqlCommandcmd=newSqlCommand ("WAITFORDELAY'00:00:03';", conn); conn.Open (); returnthis.Async (cmd.BeginExecuteNonQuery (asyncCallback,asyncState), (ar) = > {intvalue=cmd.EndExecuteNonQuery (ar); conn.Close (); returnthis.View ();});}

So far, it seems that AsyncMvcHandler has no secrets to speak of:

PublicclassAsyncMvcHandler:IHttpAsyncHandler,IRequiresSessionState {publicAsyncMvcHandler (Controllercontroller, IControllerFactorycontrollerFactory, RequestContextrequestContext) {this.Controller=controller; this.ControllerFactory=controllerFactory; this.RequestContext=requestContext;} publicControllerController {get;privateset;} publicRequestContextRequestContext {get;privateset;} publicIControllerFactoryControllerFactory {get;privateset;} publicIAsyncResultBeginProcessRequest (HttpContextcontext, AsyncCallbackcb, objectextraData) {this.Context=context; this.Controller.SetAsyncCallback (cb) .SetAsyncState (extraData); try {(this.ControllerasIController) .Execute (this.RequestContext); returnthis.Controller.GetAsyncResult () } catch {this.ControllerFactory.ReleaseController (this.Controller); throw;}} publicvoidEndProcessRequest (IAsyncResultresult) {try {HttpContext.Current=this.Context; ActionResultactionResult=this.Controller.GetAsyncEndDelegate () (result); if (actionResultNull null) {actionResult.ExecuteResult (this.Controller.ControllerContext);}} finally {this.ControllerFactory.ReleaseController (this.Controller);}

It's important to keep the current Context-- in the BeginProcessRequest method. HttpContext.Current is based on CallContext, and once the HttpContext.Current becomes null after an asynchronous callback, we have to reset it. The received AsyncCallback and AsyncState are then retained, and the controller is executed using the ready-made Execute method in the framework. When the Execute method returns, the entire process of calling the Action method is over, which means that the result of its call-- that is, the IAsyncResult and EndDelegate objects-- has been retained. So the IAsyncResult object is taken out and returned. As for the EndProcessRequest method, it simply takes out the EndDelegate saved in the BeginProcessRequest method, calls it, and executes the resulting ActionResult again.

The above code only deals with normal logic, and the complete code also includes handling for special cases such as when the Action method is terminated or replaced by a Filter. In addition, exceptions need to be handled properly in both BeginProcessRequest and EndProcessRequest, so that Controller Factory can release Controller objects in a timely manner.

If there are no defects in this solution, then it is believed that it has been put into ASP.NET MVC 1.0, and it is not my turn to expand here. The current solution has at least the following shortcomings:

Not strictly adhering to the APM pattern in .NET, although it does not affect the functionality, but this is always a pity.

By taking advantage of the off-the-shelf functionality in the framework, all Filter can only run on the BeginXxx method.

Since neither the EndXxx method nor the execution of the final ActionResult is supported by Filter, if an exception is thrown during this process, you will not be able to enter the exception handling functionality recommended by ASP.NET MVC.

According to the Roadmap,ASP.NET MVC framework of the ASP.NET MVC framework, asynchronous Action will be supported in the later version of the Action framework. It is believed that all these shortcomings can be remedied at that time. However, this requires a lot of work, which can only be left to the ASP.NET MVC team to implement slowly. In fact, you can now find asynchronous Action processing in the MvcFutures project in the ASP.NET MVC source code. It adds many extensions such as IAsyncController,AsyncController,IAsyncActionInvoker,AsyncControllerActionInvoker. Although they all "inherit" existing classes, they are similar to my previous judgment, such as AsyncControllerActionInvoker has almost completely reimplemented all the functions in ActionInvoker-- I haven't read the code carefully, so I can't tell whether the design is good or not. I just want it to be as simple and elegant as ASP.NET MVC itself.

I'm going to add Filter support to the EndXxx method of the current code, and I need to read the ASP.NET MVC source code carefully to find a solution. It is hoped that it will become a better alternative before ASP.NET MVC officially supports asynchronous Action.

On how to use the ASP.NET MVC source code to find solutions to the problem is shared here, I hope the above content can be of some help to you, if you still have a lot of doubts have not been solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report