Bob*_*Bob 1 c# asp.net asynchronous sql-server-2008
我有一个aspx页面来进行异步调用.页面从数据库获取数据(在存储过程中花费30秒),然后将结果返回给客户端.这是通过jquery帖子完成的.
问题是,虽然它执行存储过程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 { }
}
}
Run Code Online (Sandbox Code Playgroud)
原因是会话锁定.当页面打开时,会话具有读取和写入权限(通过defualt),它将锁定会话,从而阻止任何其他需要加载会话的页面.
这里有两件事.
快速解决方法是删除会话访问权限和/或将其设置为只读此页面.
由于您只返回JSON,因此您应该使用ASP.Net处理程序(ashx),因为这会产生比aspx页面更少的开销,并且默认情况下没有会话访问权限(如果需要可以添加).
另外,我不确定你为什么在这里使用异步.由于IIS必须等到页面返回响应,并且由于此页面不执行任何其他操作,因此我认为异步使用它没有任何好处.
| 归档时间: |
|
| 查看次数: |
951 次 |
| 最近记录: |