·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 封装WebService的APM为Async、Await模式利于Asp.Net页面调用
Wcf针对Async、Await指令直接可以返回Task<T>结果,但是老旧的系统中还是会有很多是在用Soap的Webservice。直接在Asp.Net页面调用APM方法确实比较麻烦,其实可以直接用TaskFactory封装APM模式为.Net4.5的async await模式,便于页面调用。
下面上实现代码,不多废话,注意注释:
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) { //这里不直接用AsyncWebService而用父类SoapHttpClientProtocol的原因是:以后可以针对不同的webservice复用 SoapHttpClientProtocol soapHttpClient = new global::AsyncWebService("http://localhost:3115/AsyncWebService.asmx"); //反射创建APM方法异步委托 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>; //打印一下调用异步前线程ID StringBuilder sb = new StringBuilder(); sb.Append("<br />"); sb.Append("Befort Thread Id:" + Thread.CurrentThread.ManagedThreadId); sb.Append("<br />"); //用TaskFactory封装APM模式为.Net4.5的async await模式 string result = await Task<string>.Factory.FromAsync<string>(beginFunc, endFunc, "zhang san", null); //打印一下调用异步后线程ID sb.Append("After Thread Id:" + Thread.CurrentThread.ManagedThreadId); sb.Append("<br />"); sb.Append(result); ltlResult.Text = sb.ToString(); } }}
注意需要在Aspx前台启用Async="true"特性。
看一下最终的效果:
前台随便敲了点样式:
<div style="padding: 0;background-color: black;color: white;height:100%;width: 100%;margin: 0 auto;font-size:xx-large;"> <h6>Test Async</h6> <p style="color: yellow;"> <asp:Literal ID="ltlResult" runat="server"></asp:Literal> </p> </div>
本文代码:http://files.cnblogs.com/files/12taotie21/WebApplication1.rar