Ajax webmethod仅在第一次尝试时触发存储过程失败

本文关键字:存储过程 失败 webmethod 第一次 Ajax | 更新日期: 2023-09-27 18:14:01

我有一个webmethod,仅在第一次尝试期间(我猜在建立新连接时)未能触发存储过程。我得到Internal error server作为一个错误的Ajax消息。但是,如果我从URL地址点击return,那么存储过程就会执行,页面就会正常工作。然后,如果我保持页面不活动一段时间,然后尝试建立一个新的连接,也会出现同样的问题。

我花了最后2天试图通过检查参数来识别问题,从webmethod中删除大部分代码,最后我能够将问题追踪到这个存储过程。

CREATE PROCEDURE [dbo].[spUnionServices]
@freetext NVARCHAR(50),
@offset int,
@fetch int
AS SET NOCOUNT ON;
BEGIN
SELECT IDphoto AS IDservice, photos.IDuser, photoCaption AS serviceTitle, photoDate as serviceDate, photoFileName AS servicePhoto, 'Photo' AS service,
    photoPublished as servicePublished, inap, hashtags, KEY_TBL.RANK, screenName AS IDname
    FROM photos INNER JOIN FREETEXTTABLE(photos, (photoCaption), @freetext) AS KEY_TBL ON photos.IDphoto = KEY_TBL.[KEY]
    INNER JOIN editorDetail ON editorDetail.IDuser = photos.IDuser
    ORDER BY RANK DESC OFFSET @offset ROWS FETCH NEXT @fetch ROWS ONLY  
END
下面是我如何从webmethod 连接到存储过程
StringBuilder SBsmallSquares = new StringBuilder();
SqlConnection sqlConnection1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
           using (sqlConnection1)
           {
               SqlCommand cmd = new SqlCommand();
               SqlDataReader ReaderPopup;
               cmd.CommandText = "spUnionServices";
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Connection = sqlConnection1;
               cmd.Parameters.AddWithValue("@offset", offset);
               cmd.Parameters.AddWithValue("@fetch", fetch);
               cmd.Parameters.AddWithValue("@freetext", fts);
               sqlConnection1.Open();
               ReaderPopup = cmd.ExecuteReader();
               if (ReaderPopup.HasRows)
               {
                   while (ReaderPopup.Read())
                   {
                   //creating the string to return. Here there is no problem.
                   }
                   return SBsmallSquares.ToString();
               }
            else return string.Empty;
如果有人能找出为什么我在第一次尝试运行存储过程时遇到这个问题,我将不胜感激。谢谢!

Ajax webmethod仅在第一次尝试时触发存储过程失败

您应该重试此错误。在可用性事件(例如升级)期间,DB可以移动到不同的机器上,fdhost需要再次与SQL实例配对。配对过程通常发生在第一个自由文本查询或需要时。您可能是第一次在该过程中看到超时。它应该对你的重审有帮助。如果这种情况非常一致,那么服务中可能存在bug,请告知。我将帮助您进一步调试。

我会把它放在注释中,但是在回答中编写代码更容易。我意识到你说你在ajax中得到错误,但它发生在服务器端,并被传播回你的ajax调用。因为您的代码中没有任何异常处理,所以IIS使用泛型异常为您包装异常,并提供最少的细节。

你需要在服务器端捕获异常并检查它,这应该会给你更多的细节,希望通过洞察找出为什么会发生,然后修复它应该会容易得多。

StringBuilder SBsmallSquares = new StringBuilder();
SqlConnection sqlConnection1 = null;
try
{
    sqlConnection1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    SqlDataReader ReaderPopup;
    cmd.CommandText = "spUnionServices";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = sqlConnection1;
    cmd.Parameters.AddWithValue("@offset", offset);
    cmd.Parameters.AddWithValue("@fetch", fetch);
    cmd.Parameters.AddWithValue("@freetext", fts);
    sqlConnection1.Open();
    ReaderPopup = cmd.ExecuteReader();
    if (ReaderPopup.HasRows)
    {
        while (ReaderPopup.Read())
        {
            //creating the string to return. Here there is no problem.
        }
        return SBsmallSquares.ToString();
    }
    else return string.Empty;
}
catch (Exception ex)
{
    // write this out to a text file OR (if you can) examine the exception in the debugger
    // what you need are
    // 0. ex.StackTrace <- the full call stack that generated the exception, now you know what method call caused it
    // 1. ex.GetType().FullName <- the full type of the exception
    // 2. ex.Message <- the message in the exception
    // 3. ex.InnerException <- if this is not null then we need the type and message (1 and 2 above) again, this can recurse as many times as needed (the ex.InnerException can have an ex.InnerException.InnerException and that one can also have an InnerException, and so on)
    // 4. some exceptions have additional details in other properties depending on the type, if you see anything usefull get that too
    throw; // rethrow the exception because you do not want to ignore it and pretend everything succeeded (it didn't)
}
finally
{
    if(sqlConnection1 != null) sqlConnection1.Close();
}

编辑

也可以在IIS中更新网站配置(和web。config(我相信)来传递完整的异常和细节。我不确定它是否只是在Azure或IIS中为您托管的sql server。在后一种情况下,我不熟悉如何配置它来允许这一点,但这可能是可能的。您有时还可以指定是否只允许在本地主机上运行时完整的异常/错误详细信息,这在您进行调试时非常有用,因为您希望在故障排除时看到异常信息,但您更希望外部世界(公司之外)不看到任何敏感的内部详细信息。

如果你在Azure中运行IIS,那么在搜索中添加一个关于Azure的关键字,你可以在谷歌上搜索更多关于如何启用它的信息。Google: iis show exception

我的猜测是有一个会话身份验证方案没有被启动。您应该尝试先执行get操作,以允许在浏览器中设置cookie。或者更仔细地检查在成功尝试时设置的cookie,如果信息在页面生成时可用,则对其进行对齐。