在網(wǎng)上也找了一些,發(fā)現(xiàn)要么題目和內(nèi)容不對(duì)應(yīng),明明是在 Winform 下異步調(diào)用,卻寫(xiě)成在 Asp.net 異步調(diào)用 WebService ,有的調(diào)用方式在 .NET3.5 下不能通過(guò), .NET3.5 下取消了 BeginXXXX,EndXXXX ,奇怪,而用了以下方式。
建立一個(gè) WebService 和一個(gè) WebApplication 如圖所示:
WebService 代碼為:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebService1
{
/// <summary>
/// Service1 的摘要說(shuō)明
/// </summary>
[ WebService (Namespace = "http://tempuri.org/" )]
[ WebServiceBinding (ConformsTo = WsiProfiles .BasicProfile1_1)]
[System.ComponentModel. ToolboxItem ( false )]
// 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services. WebService
{
[ WebMethod ]
public string HelloWorld()
{
return "Hello World" ;
}
}
}
以下為 同步調(diào)用 WebService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI. Page
{
protected void Page_Load( object sender, EventArgs e)
{
// 同步調(diào)用 WebService
Response.Write( "aaa<br>" );
localhost. Service1 n = new localhost. Service1 ();
Response.Write(n.HelloWorld().ToString()+ "<br/>" );
Response.Write( "bbb<br>" );
}
}
}
輸出結(jié)果:
aaa
Hello World
bbb
以下為 異步調(diào)用 WebService
1 、需要在 Default.aspx 頁(yè)的 Page 里加入 Async="true"
<% @ Page Language ="C#" AutoEventWireup ="true" CodeBehind ="Default.aspx.cs" Inherits ="WebApplication1._Default" Async ="true" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server">
< title ></ title >
</ head >
< body >
< form id ="form1" runat ="server">
< div >
</ div >
</ form >
</ body >
</ html >
2 、在 Default.aspx.cs 里代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI. Page
{
protected void Page_Load( object sender, EventArgs e)
{
// 異步調(diào)用 WebService
Response.Write( "aaa<br>" );
localhost. Service1 n = new localhost. Service1 ();
n.HelloWorldCompleted += delegate ( object sender2, localhost. HelloWorldCompletedEventArgs e2)
{
//e.Result 獲取處理結(jié)果
Response.Write(e2.Result.ToString()+ "<br/>" );
};
n.HelloWorldAsync();
Response.Write( "bbb<br>" );
}
}
}
運(yùn)行結(jié)果:
aaa
bbb
Hello World
試驗(yàn)結(jié)果: 發(fā)現(xiàn) Hello World 時(shí)在輸出 aaa,bbb 之后輸出 Hello World 的。
使用總結(jié):
1、在ASPX頁(yè)面設(shè)置一個(gè)允許異步調(diào)用的屬性 Async="true"這樣就可以在這個(gè)頁(yè)面里進(jìn)行異步調(diào)用了
2、同步調(diào)用的方法和異步調(diào)用的方法不一樣,異步調(diào)用的方法是WebServic的方法名+Async()作為方法名,同步調(diào)用的方法就是WebService的方法名。
以上采用了匿名方法。將方法體的代碼和委托對(duì)象相關(guān)聯(lián),如果要單獨(dú)定義一個(gè)方法,代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI. Page
{
protected void Page_Load( object sender, EventArgs e)
{
// 異步調(diào)用 WebService
Response.Write( "aaa<br>" );
localhost. Service1 n = new localhost. Service1 ();
n.HelloWorldCompleted += new localhost. HelloWorldCompletedEventHandler (HelloWorldCompleted);
n.HelloWorldAsync();
Response.Write( "bbb<br>" );
}
// 完成事件處理方法
void HelloWorldCompleted( object sender, localhost. HelloWorldCompletedEventArgs e)
{
if (e.Error != null )
throw e.Error;
Response.Write(e.Result.ToString() + "<br>" ); //e.Result 獲取處理結(jié)果
}
}
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
