Use asynchronous Action in ASP. net mvc Futures

Source: Internet
Author: User

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.

  1. Tutorial: ASP. net mvc video tutorial
  2. Extends the asynchronous Action Function for ASP. net mvc)
  3. Extension of asynchronous Action for ASP. net mvc)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.