ここを見て始めることができました:
http://hmadrigal.wordpress.com/2012/04/23/automatic-retries-using-the-transient-fault-handling-from-enterprise-libraries-entlib/
独自の検出戦略クラスを作成するだけです。私の場合、次のようになります。
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);
};
典型的なリポジトリの方法:
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;
}