c# - 登录后阶段超时时间已过

标签 c# sql-server azure entity-framework asp.net-core

我在 Azure 中有一个应用服务,连接到 Azure 中的 SQL Server 数据库。应用服务有多个与数据库交互的计时器触发的 WebJobs。

每次执行 SQL 请求时,我都会遇到以下异常,并且现有的建议都没有帮助我:

Microsoft.Data.SqlClient.SqlException (0x80131904): Connection Timeout Expired. The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=124; handshake=49; [Login] initialization=0; authentication=0; [Post-Login] complete=14011;

应用服务层为 S1,并且已启用“始终开启”。 SQL 层是“通用 - 无服务器:标准系列(Gen5),1 个 vCore”。

无论数据如何,每次都会发生错误。昨天它发生在5条记录的数据大小上。根据 App Insights,SQL Server 请求花了 14 秒才收到此错误。

当我使用相同的 Azure DB 在我的 PC 上本地运行应用程序时,永远不会发生该错误。这样它就可以毫不费力地处理数万条记录,所以我不认为这是数据库配置问题。

我最好的猜测是我的应用程序中的连接生命周期管理存在某种问题,因为它在立即执行 WebJob 时在本地工作,但在启动后半天过去后在 Azure 上崩溃。我使用 EF Core,我注册了这样的应用程序上下文:

collection
        .AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                context.Configuration.GetConnectionString("Database"),
                sqlServerOptions =>
                {
                    sqlServerOptions.CommandTimeout(3600);
                    sqlServerOptions.EnableRetryOnFailure(5, TimeSpan.FromSeconds(30), null);
                }))

...所有使用数据库上下文的服务都注册为“范围”。

也可能是 SQL Server 防火墙问题,但它确实启用了“允许 Azure 服务和资源访问此服务器”选项。

最佳答案

the SQL Server request took 14 seconds before receiving this error.

默认的 ConnectionTimeout 为 15 秒。已暂停的无服务器数据库通常需要比取消暂停更长的时间。

If a serverless database is paused, then the first login will resume the database and return an error stating that the database is unavailable with error code 40613. Once the database is resumed, the login must be retried to establish connectivity. Database clients with connection retry logic should not need to be modified. For connection retry logic options that are built-in to the SqlClient driver, see configurable retry logic in SqlClient.

Serverless Compute Tier - Connectivity

关于c# - 登录后阶段超时时间已过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76044773/

相关文章:

c# - 与 Autofac 最相似的 Java IoC 容器

sql - SSIS 日期函数

sql-server - sql触发器-按时间限制

git - Azure - 从 Bitbucket 部署

sql-server - 使用 pyodbc 从 Azure Synapse Notebook 连接到 SQL Server 时出错

c# - 为什么我的 MVC 应用程序中的日期格式错误?

c# - 如何使用适当的 cultureinfo 将字符串转换为 double

c# - 在 C# 中测试对象是否为泛型类型

c# - 如何抛出下溢异常?

sql - ISNULL 在 SQL Server 2008 中给出错误的值