英文:
Polly and async method: cannot convert async lambda expression to delegate Task<HttpResponseMessage>
问题
I am trying to use Polly to wrap an async method. How I get an error with my method, I try this:
我尝试使用Polly来包装一个异步方法。当我使用这个方法时出现错误:
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)
.
但是我遇到一个错误,错误信息说它无法将异步 lambda 表达式转换为 Task<HttpResponseMessage>
委托,错误出现在 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
.
我看不出问题在哪里,我在 ExecuteAsync
的参数中使用了 async
和 await
。
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));
英文:
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));
答案1
得分: 3
根据提供的信息,我假设您已经定义了一个策略,其中要被装饰的异步方法应返回一个HttpResponseMessage
。
Task.Delay
并不返回HttpResponseMessage
。您的策略定义必须与要被装饰的方法返回类型兼容。
英文:
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.
Definition
The definition is done via several builder functions. In all cases you have to define the following three:
- The to-be-decorated action is sync/async and method/function
- The trigger(s) for which the policy should be fire
- The chosen resilience action and its configuration
In case of your _politicaReintentar
these are the following:
HttpPolicyExtensions.HandleTransientHttpError()
It is equivalent with the following:
Policy<HttpResponseMessage>
.Handle<HttpRequestException>()
.OrTransientHttpStatusCode()
- The to-be-decorated function should return a
HttpResponseMessage
- It should trigger either for
HttpRequestException
- Or it should trigger if the status code is 408 or 5xx
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(retryAttempt));
- It says that the to-be-decorated method is async
- The retry policy should be used as a chosen resiliency action
- With at most 3 retry attempts, with predefined sleep duration between attempts
If you are interested more about this, please check the following SO topics:
- https://stackoverflow.com/questions/73095394/polly-waitandretryasync-vs-waitandretry/73095879#73095879
- https://stackoverflow.com/questions/75392165/using-polly-to-retry-one-time-with-a-200-ms-wait-on-an-async-method/75396161#75396161
Execution
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.
Back to your example
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论