[英]Retry Patterns using Polly httpClient ConfigureAwait(true)
var appendFileResponse = await Policy
.HandleResult<HttpResponseMessage>(message => !message.IsSuccessStatusCode)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
.ExecuteAsync(async () => await httpClient.SendAsync(request).ConfigureAwait(true))
.ConfigureAwait(true);
if (appendFileResponse.StatusCode != System.Net.HttpStatusCode.Accepted)
{
throw new ApplicationException($"Failed to append file");
}
我想确保 appendFileResponse 在执行 If 检查和其他一些操作之前完成。
我应该在 httpClient.SendAsync 调用中添加 ConfigureAwait(true) 和/或在 await Policy 中添加.ConfigureAwait(true) 吗?
ConfigureAwait
对运行的顺序没有影响。您使用await
的事实意味着代码将在此时“异步等待”。
ConfigureAwait
只是确定您的代码在哪个上下文中恢复:捕获的上下文(默认)或线程池上下文(如果您传递false
)。
if
在所有 Polly 重试后运行,无论您是否使用ConfigureAwait
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.