asp.net 3.5异步调用方法小结
web请求
RssAsync.aspx
//这是调用本网站的两个页面,也可以设置成外部网站页面
const string rssFeed1 = "http://localhost:2289/AsnicTest/WebRequest1.aspx";
const string rssFeed2 = "http://localhost:2289/AsnicTest/WebRequest2.aspx";
AsyncReader rss1, rss2;
protected void Page_Load(object sender, EventArgs e)
{
PreRenderComplete += new EventHandler(RssAsync_PreRenderComplete);
rss1 = new AsyncReader(rssFeed1);
rss2 = new AsyncReader(rssFeed2);
PageAsyncTask task1 = new PageAsyncTask(new BeginEventHandler(rss1.BeginRead), new EndEventHandler(rss1.EndRead), null, null, true);
PageAsyncTask task2 = new PageAsyncTask(new BeginEventHandler(rss2.BeginRead), new EndEventHandler(rss2.EndRead), null, null, true);
RegisterAsyncTask(task1);
RegisterAsyncTask(task2);
}
void RssAsync_PreRenderComplete(object sender, EventArgs e)
{
Literal1.Text = rss1.GetData();
Literal1.Text += rss2.GetData();
}
AsyncReader是封装的一个类,代码如下:const string rssFeed1 = "http://localhost:2289/AsnicTest/WebRequest1.aspx";
const string rssFeed2 = "http://localhost:2289/AsnicTest/WebRequest2.aspx";
AsyncReader rss1, rss2;
protected void Page_Load(object sender, EventArgs e)
{
PreRenderComplete += new EventHandler(RssAsync_PreRenderComplete);
rss1 = new AsyncReader(rssFeed1);
rss2 = new AsyncReader(rssFeed2);
PageAsyncTask task1 = new PageAsyncTask(new BeginEventHandler(rss1.BeginRead), new EndEventHandler(rss1.EndRead), null, null, true);
PageAsyncTask task2 = new PageAsyncTask(new BeginEventHandler(rss2.BeginRead), new EndEventHandler(rss2.EndRead), null, null, true);
RegisterAsyncTask(task1);
RegisterAsyncTask(task2);
}
void RssAsync_PreRenderComplete(object sender, EventArgs e)
{
Literal1.Text = rss1.GetData();
Literal1.Text += rss2.GetData();
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Text;
/// <summary>
///AsyncReader 的摘要说明
/// </summary>
public class AsyncReader
{
private WebRequest _req;
private string _rssData;
private string _url;
public AsyncReader(string url)
{
_url = url;
}
public IAsyncResult BeginRead(object sender, EventArgs e, AsyncCallback cb, object state)
{
_req = WebRequest.Create(_url);
IAsyncResult obj = _req.BeginGetResponse(cb,state);
return obj;
}
public void EndRead(IAsyncResult ar)
{
String str;
using (WebResponse response = _req.EndGetResponse(ar))
{
StreamReader reader;
using (reader = new StreamReader(response.GetResponseStream()))
{
str = reader.ReadToEnd();
}
_rssData = formateStr(str);
}
}
/// <summary>
/// 格式化字符,比如xml格式等。这里我就直接返回了,实例而已
/// </summary>
/// <returns></returns>
private string formateStr(string str)
{
return str;
}
/// <summary>
/// 获取异步调用返回的数据
/// </summary>
/// <returns></returns>
public string GetData()
{
return _rssData;
}
}
WebService异步调用
1.asp.net 2.0开始,提供简单的方法实现异步调用
AsyncWebService.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
this.getHello();
}
private void getHello()
{
webServerAsync.WebService1 hello = new webServerAsync.WebService1();
///同步调用WebService方法
//Literal1.Text = hello.HelloAsync("张三", 30);
//异步调用WebService方法
hello.HelloWorldCompleted += new webServerAsync.HelloWorldCompletedEventHandler(hello_HelloWorldCompleted);
hello.HelloWorldAsync("张三", 20);
}
void hello_HelloWorldCompleted(object sender, webServerAsync.HelloWorldCompletedEventArgs e)
{
Literal1.Text = e.Result;
}
{
this.getHello();
}
private void getHello()
{
webServerAsync.WebService1 hello = new webServerAsync.WebService1();
///同步调用WebService方法
//Literal1.Text = hello.HelloAsync("张三", 30);
//异步调用WebService方法
hello.HelloWorldCompleted += new webServerAsync.HelloWorldCompletedEventHandler(hello_HelloWorldCompleted);
hello.HelloWorldAsync("张三", 20);
}
void hello_HelloWorldCompleted(object sender, webServerAsync.HelloWorldCompletedEventArgs e)
{
Literal1.Text = e.Result;
}
为了调用方便,以及代码重用,我也封装了一个类
AsyncWebService.aspx.cs
helloRader hello1, hello2;
public string helloData;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
dataBind();
}
private void dataBind()
{
PreRenderComplete += new EventHandler(RssAsync_PreRenderComplete);
hello1 = new helloRader("张三", 20);
hello2 = new helloRader("李四", 22);
PageAsyncTask task1 = AsyncTask(hello1);
PageAsyncTask task2 = AsyncTask(hello2);
RegisterAsyncTask(task1);
RegisterAsyncTask(task2);
}
private PageAsyncTask AsyncTask(helloRader hello)
{
return new PageAsyncTask(new BeginEventHandler(hello.BeginRead), new EndEventHandler(hello.EndRead), null, null, true);
}
void RssAsync_PreRenderComplete(object sender, EventArgs e)
{
Literal1.Text = hello1.GetData() + "</br>";
Literal1.Text += hello2.GetData();
}
public string helloData;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
dataBind();
}
private void dataBind()
{
PreRenderComplete += new EventHandler(RssAsync_PreRenderComplete);
hello1 = new helloRader("张三", 20);
hello2 = new helloRader("李四", 22);
PageAsyncTask task1 = AsyncTask(hello1);
PageAsyncTask task2 = AsyncTask(hello2);
RegisterAsyncTask(task1);
RegisterAsyncTask(task2);
}
private PageAsyncTask AsyncTask(helloRader hello)
{
return new PageAsyncTask(new BeginEventHandler(hello.BeginRead), new EndEventHandler(hello.EndRead), null, null, true);
}
void RssAsync_PreRenderComplete(object sender, EventArgs e)
{
Literal1.Text = hello1.GetData() + "</br>";
Literal1.Text += hello2.GetData();
}
helloRader.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
///helloRader 的摘要说明
/// </summary>
public class helloRader
{
private webServerAsync.WebService1 _req;
private string _rssData;
private string _user;
private int _age;
public helloRader(string user,int age)
{
//
//TODO: 在此处添加构造函数逻辑
//
_user = user;
_age = age;
}
public IAsyncResult BeginRead(object sender, EventArgs e, AsyncCallback cb, object state)
{
_req = new webServerAsync.WebService1();
IAsyncResult obj = _req.BeginHelloWorld(_user,_age,cb, state);
return obj;
}
public void EndRead(IAsyncResult ar)
{
String str;
str = _req.EndHelloWorld(ar);
_rssData = str;
}
/// <summary>
/// 格式化字符,比如xml格式等。这里我就直接返回了,实例而已
/// </summary>
/// <returns></returns>
private string formateStr(string str)
{
return str;
}
/// <summary>
/// 获取异步调用返回的数据
/// </summary>
/// <returns></returns>
public string GetData()
{
return _rssData;
}
}