I have a aspx page to do asynchronous calls. The page gets data from the database(spends 30 seconds in the stored procedure) and then returns the results to the client. This is done through a jquery post.
The problem is that while it does the stored procedure for 30 seconds, the rest of my site can’t make any requests.
I have <%@ Page Async="true" AsyncTimeout="600" Language="C#"... in the .aspx
My .aspx.cs contains:
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 { }
}
}
The reason for this is session locking. When a page opens with Read and Write access to the session (which is by defualt), it will lock the session preventing any other pages that require the session from loading.
So two things here.
A quick fix is to drop the session access and / or set it to read only for this page.
Since you’re only returning JSON, you should use an ASP.Net handler (ashx) as this produces less overhead than an aspx page and, by default, has no session access (this can be added if necessary).
Also, I’m not sure why you’re using async here. Since IIS has to wait until the page returns a response and since this page is doing nothing else, I see no benefit to using it asynchronously.