I am trying to wrap two Polly policies and want to return IAsyncPolicy
, but it giving error,
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);
}
How to wrap this and return?
Adapt your code as follows:
Explanation
IAsyncPolicy
.IAsyncPolicy<HttpResponseMessage>
, as it handlesHttpResponseMessage
results.To combined these in a PolicyWrap, use the
.Wrap(...)
instance-method syntax:As the resulting PolicyWrap handles
HttpResponseMessage
, it is also of typeIAsyncPolicy<HttpResponseMessage>
, so the method return type changes to that.Polly documentation covers the differences between non-generic and generic policies in the wiki.