使用SQL Azure时,如果我创建自己的RetryPolicy,例如:
var retryStrategy = new FixedInterval(3, TimeSpan.FromSeconds(10));
var retryPolicySQL = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(retryStrategy);
retryPolicySQL.Retrying += ....;
我可以在重试时收到通知,这对记录很有用。
但是,如果我使用EF6和Azure的新推荐策略 - 即自定义DbConfiguration类:
public class MyConfiguration : DbConfiguration
{
public MyConfiguration()
{
this.SetExecutionStrategy("System.Data.SqlClient", () =>
{
var strat = new SqlAzureExecutionStrategy();
// strat. No events
return strat;
});
}
}
我似乎无法找到一种方法来进入重试过程。有没有办法做到这一点?
答案 0 :(得分:4)
通过从SqlAzureExecutionStrategy继承来实现名为ShouldRetryOn的受保护方法。在该方法中,您可以将逻辑记录或挂钩到处理程序中,如下所示。
public delegate void ChangedEventHandler(object sender, EventArgs e);
public class MyStrategy : SqlAzureExecutionStrategy
{
public event ChangedEventHandler Changed;
protected override bool ShouldRetryOn(Exception exception)
{
OnChanged(EventArgs.Empty);
return base.ShouldRetryOn(exception);
}
protected virtual void OnChanged(EventArgs e)
{
if (Changed != null)
Changed(this, e);
}
}
答案 1 :(得分:0)
如果您只想记录异常或重试,则可以执行以下操作:
public class LoggedSqlAzureExecutionStrategy : SqlAzureExecutionStrategy
{
protected override bool ShouldRetryOn(Exception exception)
{
var shouldRetry = base.ShouldRetryOn(exception);
if (shouldRetry)
{
// log retry
}
return shouldRetry;
}
}