4

EF 4 を使用するリポジトリに依存するアプリがいくつかあります。場合によっては、SQL 操作が失敗することがあります (つまり、タイムアウト、接続の失敗など)。

Transient Fault Handling Application Block を使用したいのですが、Azure を使用していません。Azure シナリオに関する情報は急増しているようですが、「ベアボーン」アプローチの使用に関する情報はありません。Azure の検出戦略を使用してもうまくいかないのではないかと心配しています。

これを最適に使用する方法に関する情報がどこにあるか知っている人はいますか?

4

1 に答える 1

2

ここを見て始めることができました: 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;
    }
于 2013-05-09T21:31:45.127 に答える