C# 为什么EF Core在失败时重试不起作用

C# 为什么EF Core在失败时重试不起作用,c#,entity-framework,asp.net-core,interceptor,ef-core-3.1,C#,Entity Framework,Asp.net Core,Interceptor,Ef Core 3.1,我已将我的db上下文配置为在出现故障时通过以下方式重试: optionsBuilder.UseSqlServer(connectionString,SQLServerOptions操作:sqlOptions=>{ sqlOptions.EnableRetryOnFailure( 最大计数:10, maxRetryDelay:TimeSpan.FromSeconds(30), errorNumbersToAdd:null); }); 我还配置了以下拦截器: 公共类DbCommandFailure

我已将我的db上下文配置为在出现故障时通过以下方式重试:

optionsBuilder.UseSqlServer(connectionString,SQLServerOptions操作:sqlOptions=>{
sqlOptions.EnableRetryOnFailure(
最大计数:10,
maxRetryDelay:TimeSpan.FromSeconds(30),
errorNumbersToAdd:null);
});
我还配置了以下拦截器:

公共类DbCommandFailureInterceptor:DbCommandInterceptor
{
private int retryCount=2;
公共重写异步任务NonQueryExecutingAsync(DbCommand命令、CommandEventData eventData、InterceptionResult结果、CancellationToken CancellationToken=default)
{
投掷(命令);
返回结果;
}
公共重写异步任务ReaderExecutingAsync(DbCommand命令、CommandEventData事件数据、InterceptionResult结果、CancellationToken CancellationToken=default)
{
投掷(命令);
返回结果;
}
私有无效抛出(DbCommand)
{
if(!(command.CommandText.Contains(“服务器属性”)
||command.CommandText.Contains(“\u MigrationHistory”))
&&retryCount>0){
--复述计数;
抛出SqlExceptionFaker.Error10053;
}
}
}
因此,基本上我希望在从数据库中获取实体时,3次命中Throw方法,2次命中Throw方法并引发异常,最后一次命中Throw方法并没有引发异常。 但实际上,在失败时并没有重试,并且在第一个异常执行停止之后。 我做错了什么

我有这样一个支持弹性交易的助手:

公共类ResilientTransaction
{
私有只读DbContext\u context;
私有ResilientTransaction(DbContext上下文)=>
_context=context??抛出新ArgumentNullException(nameof(context));
公共异步任务ExecuteAsync(Func操作)
{
var strategy=_context.Database.CreateExecutionStrategy();
等待策略。ExecuteAsync(异步()=>{
使用(var transaction=await_context.Database.BeginTransactionAsync()){
等待行动();
wait transaction.CommitAsync();
}
});
}
公共静态弹性Transaction New(DbContext上下文)=>
新的弹性交易(上下文);
}
下面是我执行它的方式:

wait ResilientTransaction.New(_dbContext).ExecuteAsync(async()=>{
_添加(新租户());
wait_dbContext.saveChangesSync();
});

您是否遇到了故障?为什么会失败?我想如果retryCount==0应该抛出异常,否则将不会处理抛出。@jdweng确定我收到异常,并且由于某种原因没有任何内容保存到数据库中,这是最令人沮丧的部分,这就是问题所在-为什么失败?您是否检查了SQL Server上的日志文件?请看:@jdweng您能更具体地指出我应该注意哪些日志文件吗?您是否遇到了故障?为什么会失败?我想如果retryCount==0应该抛出异常,否则将不会处理抛出。@jdweng确定我收到异常,并且由于某种原因没有任何内容保存到数据库中,这是最令人沮丧的部分,这就是问题所在-为什么失败?您是否检查了SQL Server上的日志文件?请看:@jdweng您能更具体地指出我应该注意哪些日志文件吗?