The APM encapsulation of WebService is Async, And the Await mode facilitates Asp. Net page calls, And asyncawait
For Async and Await commands, Wcf can directly return the Task <T> result, but there are still many WebServices using Soap in old systems. Calling the APM method directly on the Asp. Net page is really troublesome. In fact, you can directly use TaskFactory to encapsulate the async await mode in the APM mode of. Net4.5 to facilitate page calls.
The implementation code below is not nonsense. Note the following:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading; using System. threading. tasks; using System. web; using System. web. services. protocols; using System. web. UI; using System. web. UI. webControls; namespace WebApplication1 {public partial class _ Default: Page {protected async void Page_Load (object sender, EventArgs e) {// The reason why the parent SoapHttpClientProtocol is used for different WebServices instead of AsyncWebService is not used here is that SoapHttpClientProtocol soapHttpClient = new global: AsyncWebService (" http://localhost:3115/AsyncWebService.asmx "); // Create an APM method asynchronously delegate var beginFunc = soapHttpClient. getType (). getMethod ("BeginHelloWorld "). createDelegate (typeof (Func <string, System. asyncCallback, object, IAsyncResult>), soapHttpClient) as Func <string, System. asyncCallback, object, IAsyncResult>; var endFunc = soapHttpClient. getType (). getMethod ("EndHelloWorld "). createDelegate (typeof (Func <IAsyncResult, string>), soapHttpClient) as Func <IAsyncResult, string>; // print the thread ID StringBuilder sb = new StringBuilder () before the asynchronous call (); sb. append ("<br/>"); sb. append ("Befort Thread Id:" + Thread. currentThread. managedThreadId); sb. append ("<br/>"); // use TaskFactory to encapsulate the APM mode. net4.5 async await mode string result = await Task <string>. factory. fromAsync <string> (beginFunc, endFunc, "zhang san", null); // print the thread ID sb after the asynchronous call. append ("After Thread Id:" + Thread. currentThread. managedThreadId); sb. append ("<br/>"); sb. append (result); ltlResult. text = sb. toString ();}}}
Note that you must enable the Async = "true" feature in the Aspx foreground.
Let's take a look at the final effect:
The front-end randomly clicks the style:
<Div style = "padding: 0; background-color: black; color: white; height: 100%; width: 100%; margin: 0 auto; font-size: xx-large; ">
<H6> Test Async <P style = "color: yellow;">
<Asp: Literal ID = "ltlResult" runat = "server"> </asp: Literal>
</P>
</Div>
Code: http://files.cnblogs.com/files/12taotie21/WebApplication1.rar