尝试包装 2 个 Polly 策略时出现转换错误
getting conversion error while trying to wrap 2 Polly policies
我正在尝试包装两个 Polly 策略并想要 return IAsyncPolicy
,但它给出了错误,
convert from Polly.Retry.RetryPolicy < System.Net.Http.HttpResponseMessage> to Polly.IAsyncPolicy
public static IAsyncPolicy CreateDefaultRetryPolicy()
{
var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromSeconds(180));
var waitAndRetryPolicy = Polly.Policy
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)),
(result, timeSpan, context) =>
{
});
return Policy.WrapAsync(timeoutPolicy, waitAndRetryPolicy);
}
如何包装这个和return?
按如下方式调整您的代码:
public static IAsyncPolicy<HttpResponseMessage> CreateDefaultRetryPolicy()
{
var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromSeconds(180));
var waitAndRetryPolicy = Polly.Policy
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)),
(result, timeSpan, context) =>
{
});
return timeoutPolicy.WrapAsync(waitAndRetryPolicy);
}
说明
- 超时策略是非通用的,
IAsyncPolicy
。
- 等待重试策略是通用的,
IAsyncPolicy<HttpResponseMessage>
,因为它处理 HttpResponseMessage
个结果。
要将这些组合到 PolicyWrap 中,请使用 .Wrap(...)
实例方法语法:
return timeoutPolicy.WrapAsync(waitAndRetryPolicy);
由于生成的 PolicyWrap 处理 HttpResponseMessage
,它也是类型 IAsyncPolicy<HttpResponseMessage>
,因此方法 return 类型更改为该类型。
Polly 文档涵盖了 wiki.
中非通用策略和通用策略之间的差异
我正在尝试包装两个 Polly 策略并想要 return IAsyncPolicy
,但它给出了错误,
convert from Polly.Retry.RetryPolicy < System.Net.Http.HttpResponseMessage> to Polly.IAsyncPolicy
public static IAsyncPolicy CreateDefaultRetryPolicy()
{
var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromSeconds(180));
var waitAndRetryPolicy = Polly.Policy
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)),
(result, timeSpan, context) =>
{
});
return Policy.WrapAsync(timeoutPolicy, waitAndRetryPolicy);
}
如何包装这个和return?
按如下方式调整您的代码:
public static IAsyncPolicy<HttpResponseMessage> CreateDefaultRetryPolicy()
{
var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromSeconds(180));
var waitAndRetryPolicy = Polly.Policy
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)),
(result, timeSpan, context) =>
{
});
return timeoutPolicy.WrapAsync(waitAndRetryPolicy);
}
说明
- 超时策略是非通用的,
IAsyncPolicy
。 - 等待重试策略是通用的,
IAsyncPolicy<HttpResponseMessage>
,因为它处理HttpResponseMessage
个结果。
要将这些组合到 PolicyWrap 中,请使用 .Wrap(...)
实例方法语法:
return timeoutPolicy.WrapAsync(waitAndRetryPolicy);
由于生成的 PolicyWrap 处理 HttpResponseMessage
,它也是类型 IAsyncPolicy<HttpResponseMessage>
,因此方法 return 类型更改为该类型。
Polly 文档涵盖了 wiki.
中非通用策略和通用策略之间的差异