我們使用 Entity Framework 6.4.4(非核心)托管一個 .NET5 WebApi(ASP.NET Core)并連接到 Azure SQL 資料庫。
每隔一段時間,對我們的 API 的呼叫就會因為這個例外而失敗:
The underlying provider failed on Open. Login failed for user 'myuser'.
我們知道我們擁有正確的用戶名和密碼,因為錯誤會在幾秒鐘后自行消失。我們正在使用SqlAzureExecutionStrategy,但這不是重試該特定錯誤代碼 (18456)。
什么是解決這個問題的好方法?我們應該簡單地重試這個錯誤代碼,還是有更好的解決方案?
我們目前設定了以下配置:
[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);
}
}
uj5u.com熱心網友回復:
我自己對 wordaround 的最佳猜測是擴展執行策略。
我在 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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317469.html
標籤:C# 天蓝色 实体框架 azure-sql-数据库
下一篇:沒有找到org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor類的序列化器,兩個方法的結果相同,但輸出不同
