我有一個aspx頁面來執行異步調用。頁面從數據庫獲取數據(在存儲過程中花費30秒),然後將結果返回給客戶端。這是通過一個jQuery文章完成的。Asp.Net異步頁面停止其他請求
問題是,雖然它執行存儲過程30秒,我的網站的其餘部分不能提出任何請求。
我<%@ Page Async="true" AsyncTimeout="600" Language="C#"...
在.aspx
我.aspx.cs包含:
private SqlConnection _connection;
private SqlCommand _command;
private SqlDataReader _reader;
protected void Page_Load(object sender, EventArgs e)
{
AddOnPreRenderCompleteAsync(BeginAsyncOperation, EndAsyncOperation);
}
protected IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
{
IAsyncResult result = null;
try
{
SqlConnectionStringBuilder ConnectionStringBuilder = new SqlConnectionStringBuilder("**MyConnectionStringGoesHere**");
ConnectionStringBuilder.AsynchronousProcessing = true;
_connection = new SqlConnection(ConnectionStringBuilder.ConnectionString);
_command = new SqlCommand("GetSiteUpdates", _connection) { CommandType = CommandType.StoredProcedure };
_connection.Open();
result = _command.BeginExecuteReader(cb, state);
return result;
}
catch (Exception ex)
{}
return result;
}
protected void EndAsyncOperation(IAsyncResult ar)
{
_reader = _command.EndExecuteReader(ar);
try{
//Do Code Here
//Set Builder string here
Response.Clear();
Response.Write(builder.ToString());
_reader.Close();
Response.End();
}
catch (System.Threading.ThreadAbortException)
{
try { _reader.Close(); }
catch { }
}
catch (Exception ex)
{
try { _reader.Close(); }
catch { }
}
}
我決定讓它是異步的,因爲數據庫需要30秒才能返回數據,並且在那30秒內,我希望我的線程在服務器上響應其他用戶的其他請求等。 – Bob
根據http:///msdn.microsoft.com/en-us/library/ms178581.aspx,對不同會話ID的請求可以同時訪問會話。只有另一個相同會話ID的請求才需要等待鎖定。 – mbeckish
謝謝,這正是我需要的 - 用ashx選項。 – Bob