简体   繁体   English

如何在执行C#Azure功能期间避免SqlClient超时错误?

[英]How can I avoid a SqlClient timeout error during execution of a C# Azure Function?

I have created a function app time trigger with C#. 我用C#创建了一个函数app time触发器。 Inside the logic, I call and execute a stored procedure that I have created in SQL Server. 在逻辑内部,我调用并执行我在SQL Server中创建的存储过程。

The execution of the stored proc takes more than 8 minutes, and I have got the following error in Azure function apps log : 执行存储过程需要8分钟以上,我在Azure功能应用程序日志中遇到以下错误:

2018-04-02T11:03:46.409 [Error] Exception while executing function: Functions.AbarIomWeeklyUsage. 2018-04-02T11:03:46.409 [错误]执行函数时出现异常:Functions.AbarIomWeeklyUsage。 mscorlib: Exception has been thrown by the target of an invocation. mscorlib:调用目标抛出了异常。 .Net SqlClient Data Provider: Execution Timeout Expired. .Net SqlClient数据提供程序:执行超时已过期。 The timeout period elapsed prior to completion of the operation or the server is not responding. 操作完成之前经过的超时时间或服务器没有响应。 The wait operation timed out. 等待操作超时。 2018-04-02T11:03:46.425 [Error] Function completed (Failure, Id=1b85e7ef-ea52-4fd3-ab79-f27d188c5cac, Duration=30323ms) 2018-04-02T11:03:46.425 [错误]功能完成(失败,Id = 1b85e7ef-ea52-4fd3-ab79-f27d188c5cac,持续时间= 30323ms)

So far I tried 2 things : 1. leverage the connection timeout in application connection string 2. add timeout in host.json 到目前为止,我尝试了两件事:1。利用应用程序连接字符串中的连接超时2.在host.json添加超时

But it still gives me the same error 30 seconds timeout. 但它仍然给我30秒超时相同的错误。

Anyone experienced the same or have solution for this? 有没有经历过相同或有解决方案?

You are certainly exceeding the default command timeout (the default is 30 seconds). 您肯定超过了默认命令超时(默认为30秒)。 If you don't set SqlCommand.CommandTimeout you will get a SqlException. 如果未设置SqlCommand.CommandTimeout ,则会出现SqlException。 Set the command's CommandTimeout in code (there is no configuration for this) to 10 minutes (600 seconds), same as your function timeout. 将代码中的命令的CommandTimeout(没有配置)设置为10分钟(600秒),与功能超时相同。

Here's a code snippet on how to do this: 以下是有关如何执行此操作的代码段:

using (SqlCommand cmd = new SqlCommand(text, conn))
{
        cmd.CommandTimeout = 60 * 60; // seconds
        cmd.Parameters.AddWithValue("@parameter1", myparameter1);

        // Execute the command and log the # rows affected.
        var rows = await cmd.ExecuteNonQueryAsync();

        log.Info($"{rows} rows were deleted");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM