【发布时间】:2021-08-17 21:26:26
【问题描述】:
我正在使用这个 dapper extension,它使用 polly 进行重试。
我们可以看到它定义了一个静态重试策略:
private static readonly AsyncRetryPolicy RetryPolicy =
Policy
.Handle<SqlException>(SqlServerTransientExceptionDetector.ShouldRetryOn)
.Or<TimeoutException>()
.OrInner<Win32Exception>(SqlServerTransientExceptionDetector.ShouldRetryOn)
.WaitAndRetryAsync(RetryTimes,
(exception, timeSpan, retryCount, context) =>
{
LogTo.Warning(
exception,
"WARNING: Error talking to ReportingDb, will retry after {RetryTimeSpan}. Retry attempt {RetryCount}",
timeSpan,
retryCount
);
});
理想情况下,我想设置RetryTimes。但是,我不确定如何在静态属性中将IConfiguration 传递给RetryTimes?
我能想到的工作是将 polly 策略定义为 Singleton(这样我就可以读取设置)。然后在这个扩展方法中,我实际上传递了策略。这是关于在 .NET Core 中处理它的正确方法吗?
【问题讨论】:
-
在已经获得配置访问权限后,在启动期间设置静态策略。
-
您如何在代码中使用该策略?如果您在从 DI 解析的类中使用
RetryPolicy,您还可以将重试策略解析为来自 DI 的单例。
标签: c# asp.net-core retrypolicy