C# Ajax webmethod仅在第一次尝试时无法触发存储过程

C# Ajax webmethod仅在第一次尝试时无法触发存储过程,c#,sql,asp.net,azure,azure-sql-database,C#,Sql,Asp.net,Azure,Azure Sql Database,我有一个webmethod,仅在第一次尝试时(我猜是在建立新连接时)无法启动存储过程。我得到内部错误服务器作为错误Ajax消息。但是,如果我从URL地址点击return,那么存储过程就会执行,页面也会正常工作,这是应该的。然后,如果我保持页面处于非活动状态一段时间,然后尝试建立新连接,同样的问题也会发生 在过去的两天里,我通过检查参数、从webmethod中删除大部分代码来识别问题,最后我能够将问题跟踪到此存储过程 CREATE PROCEDURE [dbo].[spUnionServices]

我有一个webmethod,仅在第一次尝试时(我猜是在建立新连接时)无法启动存储过程。我得到
内部错误服务器
作为错误Ajax消息。但是,如果我从URL地址点击return,那么存储过程就会执行,页面也会正常工作,这是应该的。然后,如果我保持页面处于非活动状态一段时间,然后尝试建立新连接,同样的问题也会发生

在过去的两天里,我通过检查参数、从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;

如果有人能在第一次尝试运行存储过程时发现我出现此问题的原因,我将不胜感激。谢谢

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

我将把它放在注释中,但在这里的答案中编写代码更容易。 我知道您说过您在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();
}
StringBuilder SBsmallSquares=新的StringBuilder();
SqlConnection sqlConnection1=null;
尝试
{
sqlConnection1=新的SqlConnection(System.Configuration.ConfigurationManager.ConnectionString[“DefaultConnection”].ConnectionString);
SqlCommand cmd=新的SqlCommand();
SqlDataReader-readerpoup;
cmd.CommandText=“spUnionServices”;
cmd.CommandType=CommandType.storedProcess;
cmd.Connection=sqlConnection1;
cmd.Parameters.AddWithValue(“@offset”,offset);
cmd.Parameters.AddWithValue(“@fetch”,fetch);
cmd.Parameters.AddWithValue(“@freetext”,fts);
sqlConnection1.Open();
readerpoup=cmd.ExecuteReader();
if(ReaderPopup.HasRows)
{
while(readerpoup.Read())
{
//创建要返回的字符串。这里没有问题。
}
返回SBsmallSquares.ToString();
}
否则返回字符串。空;
}
捕获(例外情况除外)
{
//将其写入文本文件或(如果可以)在调试器中检查异常
//你需要的是

//0.ex.StackTrace我猜有一个会话身份验证方案没有启动。您应该尝试执行get first以允许在浏览器中设置cookie。或者,对成功尝试时设置的cookie进行更仔细的检查,并在po上提供信息的情况下将其对齐页面生成的int。

当您调试服务器端webmethod代码时,会出现什么异常?我没有收到任何错误…我能捕获的唯一错误是“服务器内部错误”从Ajax webmethod无法执行此操作。服务器上出现故障。您是否能够在webmethod内设置断点并在其上停止?我设法在webmethod外运行存储过程,错误如下:全文查询过程中出现错误。常见原因包括:断字错误或超时、FDHOST权限/ACL问题、服务帐户缺少权限、IFilter故障、与FDHost和sqlservr.exe的通信通道问题等。如果我再次点击enter,则存储过程将无误运行……这只是第一次尝试。是否有帮助???此错误发生在Azure Sql V12上,它是新闻集Azure数据库。事实上,类似的行为r、 由于SQL服务无法验证签名,从SQL database 2005开始,它被认为是服务中的一个错误,然后Microsoft用更高版本解决了它。我在Azure支持论坛上问了同样的问题,但还没有人愿意回答。是的,错误非常一致……我在不同的机器上测试了该页面nd我第一次得到了相同的错误结果。您好,我是SQL DB中此功能的开发人员。请在Asp.Net网页中重试查询,我们将调查查询超时的原因,并尝试根据优先级获得修复。感谢您让我们知道。我一直在运行一个包含定期全文搜索的简单查询(每15分钟一次)。错误不再一致,这意味着90%的第一个请求(在建立新池连接时)在没有eror的情况下运行。但是与其他查询(没有全文搜索)相比,它非常慢。不包含全文搜索的查询从您请求页面到加载页面最长需要2秒。而包含全文搜索的查询的等待时间是第一次请求的5到10倍。我感谢您的回答。我想您错过了我上面关于我有一个l已在webmethod外部运行SQL查询,我从服务器收到以下错误:全文查询期间发生错误。常见原因包括:分词错误或超时、FDHOST权限/ACL问题、服务帐户缺少权限、IFilter故障、与FDHOST和sqlservr.exe的通信通道问题等等…这似乎是由于系统中的错误导致的错误