体验ASP.NET 2.0 中的异步页功能

简单介绍实现原理

  下图左为未使用异步页功能执行过程(Asp.net 1.0通常情况), 下图右为使用了异步页执行过程(Asp.net 2.0新增特性).


  1. 当未使用异步页时,一个线程只能为同一个页面的请求服务. 即使页面请求过程中处理其它的I/O等操作时,此线程也一直处于等待状态. 当此页面使用完此线程时,才将它放回到线程池. 线程数量是有限的! 所以当不使用线程时及时放回线池可以使系统性能大大提高!

  2.当使用了异步页功能时,如右图中,开始Thread1是为页面服务的,但当页面处理其它的事情(比如I/O或调用其它WebService) 时,Thread1被放回线程池, 此时Thread1可以为其它页面请求服务了. 当此页面执行完自己的操作回来后, Thread2接着为页面请求服务,并不是使用的原来的线程Thread1. 这样网站的伸缩性会更好.

  (二).使用方法示例

  I. 用 Page.AddOnPreRenderCompleteAsync 实现异步页功能

  a. Page标志加属性: Async="true", 添加后代码如下:

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="AsyncPage.aspx.cs"
Inherits="_Default" Async="true" %>

  b. 后台异步页面相关代码 :

1private WebRequest _request;
2 protected void Page_Load(object sender, EventArgs e)
3 {
4 //注册异步调用的Begin和End方法.
5 AddOnPreRenderCompleteAsync(
6 new BeginEventHandler(BeginAsyncOperation),
7 new EndEventHandler(EndAsyncOperation)
8 );
9 }
10
11 //异步调用开始方法(当执行此方法时,当前线程就回到线程池,等待为其它请求服务).
12 IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
13 {
14 _request = WebRequest.Create("http://blog.csdn.net/chengking/");
15 return _request.BeginGetResponse(cb, state);
16 }
17
18 //异步调用结束后的接收方法(异步操作执行完成后,会重新从线程池中取个线程为本页面请求服务).
19 void EndAsyncOperation(IAsyncResult ar)
20 {
21 string text;
22 using (WebResponse response = _request.EndGetResponse(ar))
23 {
24 using (StreamReader reader = new StreamReader(response.GetResponseStream()))
25 {
26 text = reader.ReadToEnd();
27 }
28 }
29 this.lbOupput.Text = text;
30 }

  2. 数据库对象SqlCommand实现异步调用功能.

  a. Page标志加属性: Async="true", 添加后代码如下:

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="AsyncPage.aspx.cs"
Inherits="_Default" Async="true" %>

  b. 后台代码

1public partial class AsyncVisitDatabase : System.Web.UI.Page
2{
3 //定义数据操作对象
4 private SqlConnection _connection;
5 private SqlCommand _command;
6 private SqlDataReader _reader;
7
8 protected void Page_Load(object sender, EventArgs e)
9 {
10 if (!IsPostBack)
11 {
12 //注册事件Page_PreRender执行完成时执行方法
13 this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
14
15 /**////注册异步调用的Begin和End方法.
16 AddOnPreRenderCompleteAsync(
17 new BeginEventHandler(BeginAsyncOperation),
18 new EndEventHandler(EndAsyncOperation)
19 );
20 }
21 }
22
23 //异步调用开始方法(当执行此方法时,当前线程就回到线程池,等待为其它请求服务).
24 IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
25 {
26 string connect = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
27 _connection = new SqlConnection(connect);
28 _connection.Open();
29 _command = new SqlCommand("select * from sales", _connection);
30 return _command.BeginExecuteReader(cb, state);
31 }
32
33 //异步调用结束后的接收方法(异步操作执行完成后,会重新从线程池中取个线程为本页面请求服务).
34 void EndAsyncOperation(IAsyncResult ar)
35 {
36 _reader = _command.EndExecuteReader(ar);
37 }
38
39 //事件Page_PreRender执行完成时执行方法,在这里可以将异步调用返回结果赋值给页面上的控件或者其它善后操作.
40 protected void Page_PreRenderComplete(object sender, EventArgs e)
41 {
42 GridView1.DataSource = _reader;
43 GridView1.DataBind();
44 }
45
46 public override void Dispose()
47 {
48 if (_connection != null)
49 _connection.Close();
50 base.Dispose();
51 }
52}

  3. 实现异步调用Webservice

  a. Page标志加属性: Async="true", 添加后代码如下:
 
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="AsyncPage.aspx.cs"
Inherits="_Default" Async="true" %>

  b.后台代码

  Webservice方法(生成数据).

1public class WebService : System.Web.Services.WebService {
2
3 public WebService () {
4
5 //Uncomment the following line if using designed components
6 //InitializeComponent();
7 }
8
9 [WebMethod]
10 public DataSet GetData() {
11 return CreateData();
12 }
13
14 private DataSet CreateData()
15 {
16 DataTable dtTypeChild = new DataTable();
17 dtTypeChild.Columns.Add(new DataColumn("TypeID", typeof(int)));
18 dtTypeChild.Columns.Add(new DataColumn("TypeDetail", typeof(string)));
19 //Add data
20 DataRow drChild1 = dtTypeChild.NewRow();
21 drChild1["TypeID"] = 1;
22 drChild1["TypeDetail"] = "Apple";
23 dtTypeChild.Rows.Add(drChild1);
24 DataRow drChild2 = dtTypeChild.NewRow();
25 drChild2["TypeID"] = 2;
26 drChild2["TypeDetail"] = "orange";
27 dtTypeChild.Rows.Add(drChild2);
28 DataRow drChild3 = dtTypeChild.NewRow();
29 drChild3["TypeID"] = 3;
30 drChild3["TypeDetail"] = "banana";
31 dtTypeChild.Rows.Add(drChild3);
32 DataRow drChild4 = dtTypeChild.NewRow();
33 drChild4["TypeID"] = 4;
34 drChild4["TypeDetail"] = "pineapple";
35 dtTypeChild.Rows.Add(drChild4);
36 DataRow drChild5 = dtTypeChild.NewRow();
37 drChild5["TypeID"] = 5;
38 drChild5["TypeDetail"] = "pear";
39 dtTypeChild.Rows.Add(drChild5);
40 dtTypeChild.TableName = "fruit";
41
42 DataSet ds = new DataSet();
43 ds.Tables.Add(dtTypeChild);
44 return ds;
45 }
46
47}

  后台代码:

1public partial class AsyncVisitWebservice : System.Web.UI.Page
2{
3 private King.WebService _ws;
4 private DataSet _ds;
5
6 protected void Page_Load(object sender, EventArgs e)
7 {
8 if (!IsPostBack)
9 {
10 this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
11 _ws = new King.WebService();
12 _ws.GetDataCompleted += new King.GetDataCompletedEventHandler(GetDataCompleted);
13 _ws.Url = new Uri(Request.Url, "WebService.asmx").ToString();
14 _ws.UseDefaultCredentials = true;
15 _ws.GetDataAsync();
16 }
17 }
18
19 void GetDataCompleted(Object source, King.GetDataCompletedEventArgs e)
20 {
21 _ds = e.Result;
22 }
23
24 protected void Page_PreRenderComplete(object sender, EventArgs e)
25 {
26 this.GridView1.DataSource = _ds;
27 GridView1.DataBind();
28 }
29
30 public override void Dispose()
31 {
32 if (_ws != null)
33 _ws.Dispose();
34 base.Dispose();
35 }
36}

  4. 用 PageAsyncTask 实现异步调用功能(除了实现异步功能,还可以实现异步操作等待时间以及时间超时操作)

  a. Page标志加属性: Async="true" AsyncTimeout="5" , 添加后代码如下:

1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AsyncPageTask.aspx.cs" Inherits="AsyncPageTask" Async="true" AsyncTimeout="5" %>

  b. 后台代码

1public partial class AsyncPageTask : System.Web.UI.Page
2{
3 private WebRequest _request;
4
5 protected void Page_Load(object sender, EventArgs e)
6 {
7 PageAsyncTask task = new PageAsyncTask(
8 new BeginEventHandler(BeginAsyncOperation),
9 new EndEventHandler(EndAsyncOperation),
10 new EndEventHandler(TimeoutAsyncOperation),
11 null
12 );
13
14 RegisterAsyncTask(task);
15 }
16
17 IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
18 {
19 _request = WebRequest.Create("http://blog.csdn.net/chengking");
20 return _request.BeginGetResponse(cb, state);
21 }
22
23 void EndAsyncOperation(IAsyncResult ar)
24 {
25 string text;
26 using (WebResponse response = _request.EndGetResponse(ar))
27 {
28 using (StreamReader reader = new StreamReader(response.GetResponseStream()))
29 {
30 text = reader.ReadToEnd();
31 }
32 }
33
34 lbDisplay.Text = text.ToString();
35 }
36
37 //时间超时执行操作
38 void TimeoutAsyncOperation(IAsyncResult ar)
39 {
40 lbDisplay.Text = "Failture!";
41 }
42}


  (三).遗留问题.

  第二种方式: 数据库对象SqlCommand实现异步调用功能

  (对应示例代码中的页面: AsyncVisitDatabase.aspx),示例代码我始终没有调试成功!

  数据库里有数据,但显示不到GridView中去.

  如哪位能调试通过,敬请指点!!!

http://msdn.microsoft.com/msdnmag/issues/05/10/WickedCode/default.aspx?fig=true#fig1

  
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页