.net 在没有Azure的情况下使用实体框架和瞬态故障处理块
我有几个应用程序依赖于使用EF4的存储库。有时,SQL操作会因为以下原因而失败(即超时、连接失败等) 我想使用瞬态故障处理应用程序块,但我没有使用Azure。Azure场景中似乎有大量信息,但没有关于使用“赤裸裸”方法的信息。我担心如果我使用Azure检测策略,它将无法工作.net 在没有Azure的情况下使用实体框架和瞬态故障处理块,.net,entity-framework,.net,Entity Framework,我有几个应用程序依赖于使用EF4的存储库。有时,SQL操作会因为以下原因而失败(即超时、连接失败等) 我想使用瞬态故障处理应用程序块,但我没有使用Azure。Azure场景中似乎有大量信息,但没有关于使用“赤裸裸”方法的信息。我担心如果我使用Azure检测策略,它将无法工作 有人知道我在哪里可以找到关于如何最好地使用它的信息吗?我可以从这里开始: 您只需编写自己的检测策略类。在我的例子中,它看起来是这样的: public class EntityFrameworkTransientErrorD
有人知道我在哪里可以找到关于如何最好地使用它的信息吗?我可以从这里开始: 您只需编写自己的检测策略类。在我的例子中,它看起来是这样的:
public class EntityFrameworkTransientErrorDetectionStrategy : ITransientErrorDetectionStrategy
{
#region ITransientErrorDetectionStrategy Members
public bool IsTransient(Exception ex)
{
if (ex is SqlException)
{
return true;
}
else
return false;
}
#endregion
}
在我的存储库构造函数中,我有以下内容:
_retryStrategy = new FixedInterval(_retryLimit, new TimeSpan(0, 0, 0, 0, 500));
_retryPolicy = new RetryPolicy<EntityFrameworkTransientErrorDetectionStrategy>(_retryStrategy); // use our custom detection strategy
_retryPolicy.Retrying += (sender, args) =>
{
// Log any retries as warnings
_logger.WarnFormat("Retry {0} of {1} due to exception: {2}", args.CurrentRetryCount, _retryLimit, args.LastException);
};
如果您想在不必包装每次调用的情况下重试,请查看我创建的库()或查看EntityFramework6的新连接弹性支持(
public override HarvesterDomain.Source SelectSource(Guid id)
{
HarvesterDomain.Source source = null;
_retryPolicy.ExecuteAction(() =>
{
var contextSource = _context.Sources.Where(s => s.Id == id).FirstOrDefault();
if (contextSource != null)
source = contextSource.ToHarvesterDomainSource();
});
return source;
}