I am trying to use Polly to wrap an async method. How I get an error with my method, I try this:
var myResult = await _politicaReintentar.ExecuteAsync(async () => await Task.Delay(1000));
But I get an error that tells it cannot convert async lambda expression to delegate Task<HttpResponseMessage>
, and the error is marked in await Task.Delay(1000)
.
In the documentation the way to use is this:
var response = await timeoutPolicy
.ExecuteAsync(
async () => await FooNotHonoringCancellationAsync(), // Execute a delegate which takes no CancellationToken and does not respond to cancellation.
);
I don't see the problem, I use async
and await
as parameters of ExecuteAsync
.
How should I call my async method?
Thanks.
EDIT: this is the policy:
_politicaReintentar = HttpPolicyExtensions
.HandleTransientHttpError() // HttpRequestException, 5XX and 408
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(retryAttempt));
Based on the so far provided information, I assume you have defined a policy where the to-be-decorated async method should return an HttpReponseMessage
.
Task.Delay
does not return with a HttpResponseMessage
. Your policy definition must be compatible with the to-be-decorated method return type.
UPDATE #1
I really would like to learn how to execute an async method with Polly and retry 3 times, for example. In this case, a basic case is to execute a Task.Delay()
I don't want to repeat here the documentation of Polly so, let me give you some really short summary.
In case of Polly the policy definition and its execution are separated.
The definition is done via several builder functions. In all cases you have to define the following three:
In case of your _politicaReintentar
these are the following:
HttpPolicyExtensions.HandleTransientHttpError()
It is equivalent with the following:
Policy<HttpResponseMessage>
.Handle<HttpRequestException>()
.OrTransientHttpStatusCode()
HttpResponseMessage
HttpRequestException
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(retryAttempt));
If you are interested more about this, please check the following SO topics:
Depending on the policy definition you can either call Execute
or ExecuteAsync
. So, you have to know upfront what do you want to decorate and define your policy accordingly.
Polly also exposes methods like ExecuteAndCapture
and ExecuteAndCaptureAsync
. The different between these and the above ones is that the latter do not throw exception rather captures either the result of the method or the exception into a PolicyResult
object.
Retrying the Task.Delay
unconditionally is not supported by Polly. You have to have either a Handle<>
or HandleResult<>
to define a trigger. For the sake of simplicity I will cancel the Task.Delay
which will throw OperationCanceledException
and that would be our trigger.
var retry = Policy
.Handle<OperationCanceledException>()
.RetryAsync(3, (_, __) => Console.WriteLine("Retried"));
await retry.ExecuteAsync(async () =>
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
await Task.Delay(2000, cts.Token);
});
I've added some logging to the policy for clarity.
Here you can find the related dotnet fiddle link.