The APM encapsulation of WebService is Async, And the Await mode facilitates Asp. Net page calls, And asyncawait

Source: Internet
Author: User
Tags apm

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

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.