It is troublesome to use Asynchronization in ASP. net mvc. Several Classes supporting Asynchronization are provided in ASP. net mvc Futures starting from RC1.
Related classes include: AsyncActionDescriptor, AsyncController, AsyncControllerActionInvoker, AsyncManager, AsyncResultWrapper, AsyncTimeoutAttribute, and NoAsyncTimeoutAttribute.
Related interfaces include:: IAsyncActionDescriptor, IAsyncActionInvoker, IAsyncController, IAsyncManagerContainer.
The following describes how to use them.
I. Preparations before using asynchronous actions
1. Reference Microsoft. Web. Mvc.
2. First, the Url to be asynchronously processed should be processed by MvcHttpAsyncHandler. This step can be set by AsyncRouteCollectionExtensions. MapAsyncRoute to change the rule processed by the original MapRoute to MapAsyncRoute, for example:
routes.MapAsyncRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } ); |
3. inherit the corresponding Controller from AsyncController.
Public class HomeController: AsyncController {}
4. We agree that all the actions defined below are in HomeController.
II. The first asynchronous Action Method: Action and ActionCompleted
ASP. net mvc Futures supports automatic search for asynchronous actions by name
The usage is as follows:
Public void Async1 () {// main thread} public ActionResult Async1Completed () {// automatically searches for the Action with the main thread Action name + Completed as the asynchronous Action return Content ("Async1 "); } |
Iii. asynchronous Action Method 2: BeginAction and EndAction
If you understand the first method, the second method is also quite simple, but this method is used together with asynchronous calls of other classes.
Public delegate void AsyncEventHandler (); // a delegate is declared here, // You can also use WebRequest/WebResponse/SqlConnection to implement this asynchronous process public void Event1 () {} public IAsyncResult BeginAsync3 (AsyncCallback callback, object state) {AsyncEventHandler asy = new AsyncEventHandler (Event1); ViewData ["a"] = asy; // you must use a secondary storage object to transfer values between methods, return asy is the same in the first method. beginInvoke (callback, state);} public void EndAsync3 (IAsyncResult result) {// convert var a = ViewData ["a"] as AsyncEventHandler;. endInvoke (result); Content ("finished "). executeResult (this. controllerContext );} |
4. Third asynchronous Action Method: Use AsyncManager. RegisterTask and delegate
If the preceding two methods are difficult to implement asynchronous actions, you can use AsyncManager. RegisterTask to call the delegate to Implement Asynchronous calls.
Public void Async2 () {this. asyncManager. registerTask (c =>{// main thread, call asynchronous thread c (null) ;}, delegate (IAsyncResult result) {// asynchronous part Content ("Async2 "). executeResult (this. controllerContext );});} |
In fact, no matter which method is not perfect, I personally think the Action/ActionCompleted method may be a little more elegant, and it is only suitable for the general use of these three comparisons ). Only AsyncManager. RegisterTask is easier to pass values, while the Begin/End method is more suitable for use with other asynchronous operations.
- Tutorial: ASP. net mvc video tutorial
- Extends the asynchronous Action Function for ASP. net mvc)
- Extension of asynchronous Action for ASP. net mvc)