Web Api 上的 RetryPolicy 导致超时
RetryPolicy on Web Api causes timeout
我有一个 Web Api 调用另一个 Web api 调用来获取一些信息。为了使应用程序更具弹性,我按照以下步骤实施了 HttpTransientErrorDetectionStrategy:https://alexandrebrisebois.wordpress.com/2013/02/21/defining-an-http-transient-error-detection-strategy-for-rest-calls/
之后,我使用如下代码调用另一个网络应用程序:
RetryPolicy _retryPolicy = new RetryPolicy<HttpTransientErrorDetectionStrategy>(
new ExponentialBackoff(retryCount: 2, minBackoff: TimeSpan.FromSeconds(0), maxBackoff: TimeSpan.FromSeconds(10), deltaBackoff: TimeSpan.FromSeconds(2)));
var _httpClient = new HttpClient
{
BaseAddress = new Uri("http://www.microsoft.com")
};
HttpResponseMessage response = _retryPolicy.ExecuteAsync(async () => await _httpClient.GetAsync($"", HttpCompletionOption.ResponseContentRead)).Result;
_httpClient.GetAsync 调用卡住了,我不知道为什么。如果我删除 _retryPolicy,直接使用 _httpClient.GetAsync,它会在几秒钟内 returns。
我在控制台应用程序上有类似的代码,用于调用相同的网络应用程序,并且工作正常,所以这似乎是我在我的网络中使用它的方式所特有的 API。这本来是 Azure 上的一个应用程序,但当我在本地调试时也会发生这种情况。有人知道为什么会卡住吗?我该如何调试?
谢谢!
I have similar code on a console app, to invoke the same web app, and that is working fine, so this seems to be specific to the way I am using it in my web API.
您发布的代码在此处被阻止:
HttpResponseMessage response = _retryPolicy.ExecuteAsync(...).Result;
Don't block on async code。相反,使用 await
:
HttpResponseMessage response = await _retryPolicy.ExecuteAsync(...);
If I remove the _retryPolicy, and just use _httpClient.GetAsync directly, it returns in a matter of seconds.
如果您的原始代码是阻塞的,并且您必须阻塞异步代码(出于某种原因),那么您可以使用 ConfigureAwait(false)
hack:
HttpResponseMessage response = _retryPolicy.ExecuteAsync(async () => await _httpClient.GetAsync($"", HttpCompletionOption.ResponseContentRead).ConfigureAwait(false)).Result;
HttpResponseMessage response = _retryPolicy.ExecuteAsync(() => _httpClient.GetAsync($"", HttpCompletionOption.ResponseContentRead)).Result;
我有一个 Web Api 调用另一个 Web api 调用来获取一些信息。为了使应用程序更具弹性,我按照以下步骤实施了 HttpTransientErrorDetectionStrategy:https://alexandrebrisebois.wordpress.com/2013/02/21/defining-an-http-transient-error-detection-strategy-for-rest-calls/
之后,我使用如下代码调用另一个网络应用程序:
RetryPolicy _retryPolicy = new RetryPolicy<HttpTransientErrorDetectionStrategy>(
new ExponentialBackoff(retryCount: 2, minBackoff: TimeSpan.FromSeconds(0), maxBackoff: TimeSpan.FromSeconds(10), deltaBackoff: TimeSpan.FromSeconds(2)));
var _httpClient = new HttpClient
{
BaseAddress = new Uri("http://www.microsoft.com")
};
HttpResponseMessage response = _retryPolicy.ExecuteAsync(async () => await _httpClient.GetAsync($"", HttpCompletionOption.ResponseContentRead)).Result;
_httpClient.GetAsync 调用卡住了,我不知道为什么。如果我删除 _retryPolicy,直接使用 _httpClient.GetAsync,它会在几秒钟内 returns。
我在控制台应用程序上有类似的代码,用于调用相同的网络应用程序,并且工作正常,所以这似乎是我在我的网络中使用它的方式所特有的 API。这本来是 Azure 上的一个应用程序,但当我在本地调试时也会发生这种情况。有人知道为什么会卡住吗?我该如何调试?
谢谢!
I have similar code on a console app, to invoke the same web app, and that is working fine, so this seems to be specific to the way I am using it in my web API.
您发布的代码在此处被阻止:
HttpResponseMessage response = _retryPolicy.ExecuteAsync(...).Result;
Don't block on async code。相反,使用 await
:
HttpResponseMessage response = await _retryPolicy.ExecuteAsync(...);
If I remove the _retryPolicy, and just use _httpClient.GetAsync directly, it returns in a matter of seconds.
如果您的原始代码是阻塞的,并且您必须阻塞异步代码(出于某种原因),那么您可以使用 ConfigureAwait(false)
hack:
HttpResponseMessage response = _retryPolicy.ExecuteAsync(async () => await _httpClient.GetAsync($"", HttpCompletionOption.ResponseContentRead).ConfigureAwait(false)).Result;
HttpResponseMessage response = _retryPolicy.ExecuteAsync(() => _httpClient.GetAsync($"", HttpCompletionOption.ResponseContentRead)).Result;