对于某些请求,调用WebMethod并返回结果的过程需要较长的时间,有时,不能控制从请求数据的web服务,也不能控制这些服务的性能或响应时间,因此,应该考虑异步使用web服务,发出异步请求的ASP.NET应用程序可以在其内部的SOAP请求等待响应的同时,执行其他编程任务,在ASP。NET应用程序完成其他任务后,在从WEB服务那里获得结果.
要异步使用Web服务,可以使用BeginXXX,EndXXX方法,其中,XXX是你的Web方法名,另外,还可以使用IsCompleted来检查Web服务是否已经完成.这是一种方法,本文将讨论在.Net2.0中异步调用Web服务的另外一种方法,当新建一个WebMethod,在客户端引用这个Web服务后,除了可以看到这个Web方法外,还可以看到 XXXAsync,XXX Completed着两个方法,下面就应用这两个自动生成的方法来异步调用Web服务:
首先新建一个web服务,用于返回Sql server2000中Pubs数据库的authors表的全部记录,异步调用Web服务,web服务部分不需要做任何改动,下面是c#代码:
[WebMethod]
public DataSet BA_OperationClassGetList()
... {
string strCon = "Persist Security Info=false;Initial Catalog=pubs;User Id=sa;Password=;server=127.0.0.1";
string SQLString = "select * from authors";
SqlConnection connection = new SqlConnection(strCon);
DataSet ds = new DataSet();
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
return ds;
}
public DataSet BA_OperationClassGetList()
... {
string strCon = "Persist Security Info=false;Initial Catalog=pubs;User Id=sa;Password=;server=127.0.0.1";
string SQLString = "select * from authors";
SqlConnection connection = new SqlConnection(strCon);
DataSet ds = new DataSet();
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
return ds;
}
下面我将讲解怎么在客户端异步调用这个web服务,(以Winfom为例),首先添加该Web服务的引用,下面是客户端全部源代码:
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
... {
public partial class Form1 : Form
...{
private localhost.WebService service;
public Form1()
...{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
...{
//开始异步调用
service.BA_OperationClassGetListAsync();
}
private void service_BA_OperationClassGetListCompleted(object sender, localhost.BA_OperationClassGetListCompletedEventArgs e)
...{
DataSet ds = (DataSet)e.Result;
dataGridView1.DataSource = ds.Tables[0];
}
private void Form1_Load(object sender, EventArgs e)
...{
service = new WindowsApplication1.localhost.WebService();
service.BA_OperationClassGetListCompleted += new WindowsApplication1.localhost.BA_OperationClassGetListCompletedEventHandler(service_BA_OperationClassGetListCompleted);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
... {
public partial class Form1 : Form
...{
private localhost.WebService service;
public Form1()
...{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
...{
//开始异步调用
service.BA_OperationClassGetListAsync();
}
private void service_BA_OperationClassGetListCompleted(object sender, localhost.BA_OperationClassGetListCompletedEventArgs e)
...{
DataSet ds = (DataSet)e.Result;
dataGridView1.DataSource = ds.Tables[0];
}
private void Form1_Load(object sender, EventArgs e)
...{
service = new WindowsApplication1.localhost.WebService();
service.BA_OperationClassGetListCompleted += new WindowsApplication1.localhost.BA_OperationClassGetListCompletedEventHandler(service_BA_OperationClassGetListCompleted);
}
}
}
至此,这个异步使用web服务的代码已经书写完毕,我们就可以在等待这个web响应的同时可以显示一个滚动条或者提供一个取消按钮,可以随时终止WEB响应.