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

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)

Kri*_*ris 5

原因是会话锁定.当页面打开时,会话具有读取和写入权限(通过defualt),它将锁定会话,从而阻止任何其他需要加载会话的页面.

这里有两件事.

  1. 快速解决方法是删除会话访问权限和/或将其设置为只读此页面.

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

另外,我不确定你为什么在这里使用异步.由于IIS必须等到页面返回响应,并且由于此页面不执行任何其他操作,因此我认为异步使用它没有任何好处.

  • 根据http://msdn.microsoft.com/en-us/library/ms178581.aspx,对不同会话ID的请求可以同时访问会话.只有另一个对同一会话ID的请求才需要等待锁定. (2认同)
  • 拜伦 - 您也可以使用EnableSessionState ="ReadOnly"来解决一些情景(当只有读者需要访问特定会话的状态时). - 见http://www.drdobbs.com/windows/207800860; jsessionid = 1T3HGGTUFD4YTQE1GHRSKHWATMY32JVN (2认同)