C# 代码在部署到 IIS 时永远不会被命中(挂起)

C# code never gets hit (hang) when deployed to IIS

我正在实施一项 AJAX 服务,该服务启动长 运行 进程并通过 Javascript 的 setInterval 获取状态。完整代码在这里 http://blog.robseder.com/2013/10/18/executing-a-long-running-process-from-a-web-page/

AjaxServices.asmx 为我的 worker class 创建了一个新的静态实例。工人 class 更新其进度。问题是一切都在本地正常工作。但是它挂起并且在部署到服务器时永远不会越过线路。我正在使用 IIS 7。

using(SqlConnection conn = new SqlConnection(string))
using(SqlCommand command = conn.CreateCommand())
{
    //...
    if (conn.State != System.Data.ConnectionState.Open)
        conn.Open()
}

AjaxServices.asmx/GetStatus 工作正常...它 returns 下面圈出的状态 (HTTP 200)。问题是它根本没有命中我的 Sql 代码或 try-catch 块。

工人Class代码摘录

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
    UpdatePercent(6, 7, "Starting (inside USING)...", runId);

    #region first step
    using (SqlCommand command = conn.CreateCommand())
    {
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.CommandText = "[dbo].[sp name]";

        //parameters... etc...

        UpdatePercent(7, 8, "Parameters added, connection state: " + connectionState, runId);

        //code doesn't proceed past this line
        if (conn.State != System.Data.ConnectionState.Open)
            conn.Open();

        UpdatePercent(5, 10, "about to enter try-catch...", runId);

        try
        {
            UpdatePercent(5, 10, "Starting run insertion...", runId);
            command.ExecuteNonQuery();
            runId = int.Parse(command.Parameters["@RunID"].Value.ToString());
            UpdatePercent(10, 15, "Inserted run dimension...", runId);
        }
        catch(SqlException exc)
        {
            UpdatePercent(100, 100, exc.Message, runId);
        }
    }

将上述评论中的想法转化为此处的答案:

必须将其放入 try/catch 才能注意到抛出的 SqlException 是连接问题。

    //code doesn't proceed past this line
    if (conn.State != System.Data.ConnectionState.Open)
        conn.Open();