gpt4 book ai didi

c# - Asp.Net异步页面停止其他请求

转载 作者:太空宇宙 更新时间:2023-11-03 20:35:38 27 4
gpt4 key购买 nike

我有一个 aspx 页面来进行异步调用。该页面从数据库中获取数据(在存储过程中花费30秒),然后将结果返回给客户端。这是通过 jquery post 完成的。

问题是,当它执行存储过程 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 { }
}
}

最佳答案

这是因为 session 锁定。当一个页面打开时具有对 session 的读写访问权限(默认情况下),它将锁定该 session 以防止任何其他需要该 session 的页面加载。

这里有两件事。

  1. 一个快速的解决方法是放弃 session 访问和/或将其设置为只读该页面。

  2. 由于您只返回 JSON,因此您应该使用 ASP.Net 处理程序 (ashx),因为它比 aspx 页面产生的开销更少,并且默认情况下没有 session 访问权限(如果需要可以添加) ).

此外,我不确定您为什么要在这里使用异步。由于 IIS 必须等到页面返回响应并且此页面没有做任何其他事情,所以我认为异步使用它没有任何好处。

关于c# - Asp.Net异步页面停止其他请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5169109/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com