我必须支持一个PageAsyncTask
与 webforms vs2010 fw4 一起使用的旧项目。
然而,这个简单的代码确实编译 + 执行(在调试模式/跟踪模式下没有错误),但响应永远不会结束。
查看调试模式 + 断点 - 它到达代码的所有阶段。
public partial class _Default2 : System.Web.UI.Page
{
IAsyncResult BeginGetData(object sender, EventArgs e, AsyncCallback callback, object state)
{
SqlConnection con = new SqlConnection(@"Data Source=... Asynchronous Processing=True;");
var sql = @" SELECT [NAME] FROM [WebERP].[dbo].[t]";
{
SqlCommand _cmd = null;
try
{
_cmd = new SqlCommand(sql, con);
_cmd.CommandTimeout = 100000;
con.Open();
return _cmd.BeginExecuteReader(EndGetData, _cmd);
}
catch (Exception ex)
{
if (_cmd != null) _cmd.Dispose();
con.Close();
throw;
}
}
}
void EndGetData(IAsyncResult ar)
{
(ar.AsyncState as SqlCommand).EndExecuteReader(ar);
Response.Write(1111); // HttpContext.Current.Response also doesnt help
}
void TimeoutData(IAsyncResult ar)
{
Response.Write("Could not retrieve data!");
}
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask task = new PageAsyncTask(BeginGetData, EndGetData, TimeoutData, null, true);
Page.RegisterAsyncTask(task);
Page.ExecuteRegisteredAsyncTasks();
}
}
问题
响应永远不会结束。我所看到的是:
我错过了什么?
(nbAsync="true"
在页面指令上,也 - 代码被简化只是为了描述这种情况)