[英]Transient Login errors for Azure SQL Database
We are hosting a .NET5 WebApi (ASP.NET Core) using Entity Framework 6.4.4 (not core) and connecting to Azure SQL Database.我们正在使用 Entity Framework 6.4.4(非核心)托管 .NET5 WebApi(ASP.NET Core)并连接到 Azure SQL 数据库。
Every once in a while, calls to our API fail because of this Exception:每隔一段时间,调用我们的 API 就会因为这个异常而失败:
The underlying provider failed on Open. Login failed for user 'myuser'.
We know that we have the correct user and password because the error goes away on its own after a few seconds.我们知道我们拥有正确的用户名和密码,因为错误会在几秒钟后自行消失。 We are using
SqlAzureExecutionStrategy
, but this is not retrying that specific error code (18456).我们正在使用
SqlAzureExecutionStrategy
,但这并不是重试该特定错误代码 (18456)。
What would be a good way to solve this issue?解决这个问题的好方法是什么? Should we simply retry this error code as well, or is there a better solution?
我们是否也应该简单地重试此错误代码,还是有更好的解决方案?
We currently set the following configuration:我们目前设置了以下配置:
[DbConfigurationType(typeof(DatabaseConfiguration))]
public class MyDataContext: DbContext, IMyContext
{...}
public class DatabaseConfiguration : DbConfiguration
{
public DatabaseConfiguration()
{
SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
SetDefaultConnectionFactory(new LocalDbConnectionFactory("mssqllocaldb"));
SetProviderServices("System.Data.SqlClient", SqlProviderServices.Instance);
}
}
My own best guess for a wordaround would be to extend the execution strategy.我自己对 wordaround 的最佳猜测是扩展执行策略。
I found a somewhat similar discussion on GitHub , but it concerns AAD authentication instead of username/password.我在 GitHub 上发现了一些类似的讨论,但它涉及 AAD 身份验证而不是用户名/密码。
public class CustomExecutionStrategy : SqlAzureExecutionStrategy
{
protected override bool ShouldRetryOn(Exception ex)
{
if (ex is SqlException sqlex)
{
foreach (SqlError err in sqlex.Errors)
{
// Error number found here https://github.com/dotnet/SqlClient/issues/617#issuecomment-649686544
if (err.Number == 18456)
{
return true;
}
}
}
return base.ShouldRetryOn(ex);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.