2011-11-21 44 views
1

我針對的是隻接受單個字符串參數的Web服務。 按照計劃,我想從我的數據庫中發出大約100次來自該Web服務的100個值。 爲了優化這個過程,我相信我需要異步執行WebRequest調用。 我在各種博客上遇到過以下代碼示例等,但無法弄清楚如何根據我的要求調整。 如何在foreach循環中包裝RegisterAsyncTask,通過uri解析BeginSyncOperation內部的WebRequest.Create()?如何將PageAsyncTask與WebRequest一起用於多個請求?

using System; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net; 
using System.IO; 
using System.Text; 
using System.Text.RegularExpressions; 

public partial class AsyncPageTask : System.Web.UI.Page 
{ 
    private WebRequest _request; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     PageAsyncTask task = new PageAsyncTask(
      new BeginEventHandler(BeginAsyncOperation), 
      new EndEventHandler(EndAsyncOperation), 
      new EndEventHandler(TimeoutAsyncOperation), 
      null 
     ); 
     RegisterAsyncTask(task); 
    } 

    IAsyncResult BeginAsyncOperation(object sender, EventArgs e, 
     AsyncCallback cb, object state) 
    { 
     _request = WebRequest.Create("http://msdn.microsoft.com"); 
     return _request.BeginGetResponse(cb, state); 
    } 

    void EndAsyncOperation(IAsyncResult ar) 
    { 
     string text; 
     using (WebResponse response = _request.EndGetResponse(ar)) 
     { 
      using (StreamReader reader = 
       new StreamReader(response.GetResponseStream())) 
      { 
       text = reader.ReadToEnd(); 
      } 
     } 

     Output.Text = text; 
    } 

    void TimeoutAsyncOperation(IAsyncResult ar) 
    { 
     Output.Text = "Data temporarily unavailable"; 
    } 
} 

我的意圖是將響應字符串寫回數據庫。欣賞這是一個額外的問題,但是,是否有任何理由不在EndAsyncOperation方法中包含insert方法調用?

這個Q&A暗示我的主要問題,但哪個第四個參數?

+0

不要」如果它幫助你,忘記接受答案,作爲對他人的基本禮貌...... –

回答

2

首先,你需要添加到您的Web.config文件(默認是隻有2):

<configuration> 
    <system.net> 
     <connectionManagement> 
      <add address="*" maxconnection="100" /> 
     </connectionManagement> 
    <system.net> 
<configuration> 

然後你需要異步指令添加到您的網頁:<%@ Page Async="true" %>

protected void Page_Load(object sender, EventArgs e) 
{ 
    //...first get UriStringArray from db, and then: 
    foreach(string uri in UriStringArray) 
    { 
     var task = new PageAsyncTask(
      new BeginEventHandler(BeginAsyncOperation), 
      new EndEventHandler(EndAsyncOperation), 
      new EndEventHandler(TimeoutAsyncOperation), 
      uri, 
      true; //run in parallel 
     ); 
     RegisterAsyncTask(task); 
    } 
} 

IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state) 
{ 
    var request = (HttpWebRequest)WebRequest.Create((string)state); 
    return request.BeginGetResponse(cb, request); 
} 

void EndAsyncOperation(IAsyncResult ar) 
{ 
    string text; 
    var request = (HttpWebRequest)ar.State; 
    using(WebResponse response = request.EndGetResponse(ar)) 
    { 
     using(StreamReader reader = new StreamReader(response.GetResponseStream())) 
      text = reader.ReadToEnd(); 
    } 
    //yes, you can insert in db here, even as a new PageAsyncTask - but then must call ExecuteRegisteredAsyncTasks() manually... 
} 
+0

你知道什麼時候asynctask finsh重定向用戶。 – user960567

+0

@ user960567:精心製作,或者更好的是,顯示一些代碼 –

+0

我得到了解決方案。我可以使用PreRenderComplete事件重定向,因爲它會在完成所有異步任務後觸發。謝謝。 – user960567

相關問題