3

I have a C# code that I want to execute.

  str = "SELECT u.LastName " +
                      ",COALESCE(COUNT(DISTINCT pc.PhraseId), 0) AS CreatedByQty " +
                      ", COALESCE(COUNT(DISTINCT pm.PhraseId), 0) AS ModifiedByQty " +
                      "FROM dbo.AspNetUsers u " +
                      "LEFT JOIN dbo.Phrase pc " +
                      "ON u.Id = pc.CreatedBy " +
                      "LEFT JOIN dbo.Phrase pm " +
                      "ON u.Id = pm.ModifiedBy " +
                      "GROUP BY u.LastName ";
                threeColQuery = db.Database.SqlQuery<threeCol>(str);
                threeColReport = await threeColQuery.ToListAsync();
                return Ok(threeColReport);

I know from running the query in SQL Server manager that it takes about 30 seconds.

When running in a Web API Call I get this:

"innerException":{"message":"An error has occurred.","exceptionMessage":"The wait operation timed 

Is there a way I can allow a WebAPI call to run a long running query?

3
  • You can set this programmatically in the controller like this:- HttpContext.Server.ScriptTimeout = 300; Commented Feb 13, 2017 at 10:28
  • 1
    You can change the timeout of your DbContext like this db.Database.CommandTimeout = 60;. Would you mind posting the code of your api? Commented Feb 13, 2017 at 10:28
  • 1
    Actually I forgot to ask whether the timeout is in your client when waiting the api, or inside your api when executing the query. Commented Feb 13, 2017 at 10:29

2 Answers 2

2

Modifying the timeout settings on Entity Framework should do the trick.

db.Database.CommandTimeout = 60;

I would also advise against setting a global default timeout via the connection string. A minute is way too high for most queries, your 30-second query is an exception to the rule (thus you're knowingly increasing the timeout period).

By preventing all timeout exceptions across your app you lose a good indicator of another query misbehaving - leading to long term performance problems.

Sign up to request clarification or add additional context in comments.

Comments

0

Increase the connection timeout with Sql.

Two approach to do that

  1. Increase the connection timeout for this query like

db.Database.CommandTimeout = 60

  1. Set the timeout property globally. For that follow this answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.