我正在使用DbExecutionStrategy来使我的程序在查询服务器时支持连接超时,但是它没有按预期工作。
这是我的课:
public class MyExecutionStrategy : DbExecutionStrategy
{
static MyExecutionStrategy()
{
RetriesPerInstance = new Dictionary<int, int>();
}
private const int MaxRetryCount = 5;
private static TimeSpan _maxDelay = new TimeSpan(0, 0, 3);
private int _instanceNo;
public MyExecutionStrategy() : base(MaxRetryCount, _maxDelay)
{
_instanceNo = ++TotalInstances;
RetriesPerInstance[_instanceNo] = 0;
}
public static Dictionary<int, int> RetriesPerInstance { get; private set; }
public static int TotalRetries
{
get
{
return RetriesPerInstance.Values.Sum();
}
}
public static int TotalInstances { get; private set; }
protected override bool ShouldRetryOn(Exception ex)
{
RetriesPerInstance[_instanceNo]++;
return true;
}
}
这是我的查询:
private void Form1_Load(object sender, EventArgs xe)
{
var dengineEntities = new dengineEntities();
try
{
var proj = dengineEntities.motorista_erro.ToArray();
}
catch (Exception e)
{
var tries = MyExecutionStrategy.TotalRetries;
}
}
对于单个ToArray,它将返回42个Retries,并在引发RetryLimitExceededException之前实例化14个策略。
为什么它不止于5次尝试,又为何为单个查询创建多个实例?我有没有看到的东西?
这些是我的上下文类:
public partial class dengineEntities : DbContext
{
public dengineEntities()
: base("name=dengineEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<motorista_erro> motorista_erro { get; set; }
}
public partial class motorista_erro
{
public int MOT_ID { get; set; }
public string MOT_NOME { get; set; }
public bool MOT_SIT_FUNC_ID { get; set; }
public int IDEMP { get; set; }
}
<connectionStrings>
<add name="dengineEntities" ...etc...etc../>
</connectionStrings>
最佳答案
它在5次重试时停止,请查看RetriesPerInstance屏幕快照中的值,每个值都有6或0。6次重试意味着它重试了5次,第6次停止。
因此,听起来您的问题在其他地方。 Form1_Load被调用多少次?
关于c# - DbExecutionStrategy重试的次数超过了MaxRetryCount,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36109421/