I have read the Old Zhao articles before.Article. The implementation method of Lao Zhao Asynchronous is also studied.
However, if I feel like I have expanded myself, it is really troublesome to use Asynchronization in ASP. NET MVC. We can see that ASP. NET MVC futures has provided several classes that support Asynchronization since RC1.
Related classes includeAsyncactiondescriptor, asynccontroller, asynccontrolleractioninvoker, asyncmanager, asyncresultwrapper, asynctimeoutattribute, noasynctimeoutattribute,Related interfaces include:Iasyncactiondescriptor, iasyncactioninvoker, iasynccontroller, and iasyncmanagercontainer.
Let's talk about 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 ClassHomecontroller: 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
URL:/home/async1
The usage is as follows:
Public VoidAsync1 (){// Main thread}PublicActionresult async1completed (){// Automatically search for the action with the main thread action name + completed as the asynchronous actionReturnContent ("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.
URL:/home/async2
Public Delegate Void Asynceventhandler (); // I have declared a delegate 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; // Here, the auxiliary storage object must be used for transferring values between methods. The same is true for the first method. Return Asy. begininvoke (callback, State );} Public Void Endasync3 (iasyncresult result ){ // Switch to asynchronous action VaR A = viewdata [" A "] As Asynceventhandler; A. endinvoke (result); content (" Complete "). Executeresult ( This . Controllercontext );}4. Third asynchronous Action Method: Use asyncmanager. registertask and delegate
If the above two methods are difficult to implement asynchronous action (and indeed it is troublesome), you can use asyncmanager. registertask to call the delegate to Implement Asynchronous call.
URL:/home/async3
Public VoidAsync2 (){This. Asyncmanager. registertask (C => {// The main thread that calls the asynchronous threadC (Null);},Delegate(Iasyncresult result ){// Asynchronous partContent ("Async2"). Executeresult (This. Controllercontext );});}
In fact, either method is not perfect. I personally think the action/actioncompleted method may be more elegant and suitable for general use (only 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.
Of course, this article still has a lot to mention, including other asynchronous call methods and filters, so I hope my friends will discuss more.
2 articles by Lao Zhao
Extend the asynchronous Action Function for ASP. net mvc (I)
Extend the asynchronous Action Function for ASP. net mvc (below)
My ASP. net mvc series: Asp.net MVC Framework Series