1

我必须支持一个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"在页面指令上,也 - 代码被简化只是为了描述这种情况)

4

1 回答 1

1

我认为这里的问题是您将相同的BeginGetDataandEndGetData回调传递给BeginExecuteReaderand new PageAsyncTask()。它们应该是不同的:你传递给的东西是你传递给PageAsyncTask的东西的“外部” BeginExecuteReader

BeginExecuteReader将从传递给的 begin 回调中调用PageAsyncTask,并且您实际上需要调用AsyncCallback callbackASP.NET 提供给您的回调(您将在异步操作完成时调用它,即从您的EndGetData)。

如果您可以使用覆盖会容易得多PageAsyncTask(Func<Task>),但我认为您不能使用 VS2010 来定位 .NET 4.5。

于 2014-06-13T08:28:47.417 回答