我故意将超时设置得很低,以使http调用崩溃,以便测试重试功能,但引发了一个异常,因此httpClient没有重试上一个请求。
我起初认为,只有在maxRetry配置的一些尝试失败(HTTP代码500)之后,才会引发异常。
因此,我的代码逻辑被破坏,如果我强制执行再次执行API调用
PostAsync
,重试功能将毫无用处,代码也会变得很脏。
IClient clientV2 = new FluentClient(apiClient.getBasedUrl());
clientV2.BaseClient.Timeout = TimeSpan.FromMilliseconds(50);
clientV2.SetRequestCoordinator(
maxRetries: 3,
shouldRetry: request => request.StatusCode != HttpStatusCode.OK,
getDelay: (attempt, response) => { return TimeSpan.FromSeconds(1); } // 1, 2, and 3 seconds
);
try
{
T result = clientV2.PostAsync(_RelativeURL)
.WithHeader("APIKEY", apikey)
.WithHeader("PAYLOAD", b64)
.WithHeader("SIGNATURE", hmacSig)
.As<T>()
.Result;
dynamicJSONResult = bitfinexTicker;
} catch (Exception ex)
{
Console.WriteLine(ex);
}
系统AggregateException:发生了一个或多个错误---&燃气轮机;
---内部异常堆栈跟踪结束---
在System.Threading.Tasks.Task。ThrowifeException(布尔includeTaskCanceledException)