我想多次发送完全相同的请求,例如:
HttpClient client = new HttpClient();
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, "http://example.com");
await client.SendAsync(req, HttpCompletionOption.ResponseContentRead);
await client.SendAsync(req, HttpCompletionOption.ResponseContentRead);
Run Code Online (Sandbox Code Playgroud)
第二次发送请求将抛出异常消息:
请求消息已发送.无法多次发送相同的请求消息.
他们是一种" 克隆 "请求的方式,以便我可以再次发送?
我的实际代码HttpRequestMessage在上面的示例中设置了更多变量,变量如header和request方法.
我正在尝试将我现有的函数转换为 Polly Retry 策略
public static T Execute<T>(Func<T> getTask) where T : Task
{
var retryCount = 3;
while (retryCount-- > 0)
{
try
{
getTask().Wait();
return getTask();
} catch(Exception ex){
// handle retry
}
}
}
Run Code Online (Sandbox Code Playgroud)
转换成这个
public static T Execute<T>(Func<T> func) where T : Task
{
var task = func();
Policy.Handle<HttpRequestException>()
.Or<TimeoutException>()
.WaitAndRetryAsync(
3,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(5, retryAttempt)),
(exception, timeSpan, retryCount, context) =>
{
//do some logging
})
.ExecuteAsync(func).Wait();
return task;
}
Run Code Online (Sandbox Code Playgroud)
和测试代码是
var retryCount = 0;
var …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建重试,HttpClient DelegatingHandler以便将诸如503 Server Unavailable和超时之类的响应视为瞬态故障并自动重试.
我从http://blog.devscrum.net/2014/05/building-a-transient-retry-handler-for-the-net-httpclient/的代码开始,该代码适用于该403 Server Unavailable案例,但不处理超时作为短暂的失败.不过,我喜欢使用Microsoft瞬态故障处理块来处理重试逻辑的一般想法.
这是我目前的代码.它使用自定义Exception子类:
public class HttpRequestExceptionWithStatus : HttpRequestException {
public HttpRequestExceptionWithStatus(string message) : base(message)
{
}
public HttpRequestExceptionWithStatus(string message, Exception inner) : base(message, inner)
{
}
public HttpStatusCode StatusCode { get; set; }
public int CurrentRetryCount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这里是瞬态故障检测器类:
public class HttpTransientErrorDetectionStrategy : ITransientErrorDetectionStrategy {
public bool IsTransient(Exception ex)
{
var cex = ex as HttpRequestExceptionWithStatus;
var isTransient = cex != null …Run Code Online (Sandbox Code Playgroud) 我目前正在使用 Polly 来限制我发送的请求数量。这是我目前的政策:
private AsyncPolicyWrap<HttpResponseMessage> DefineAndRetrieveResiliencyStrategy()
{
HttpStatusCode[] retryCodes = {
HttpStatusCode.InternalServerError,
HttpStatusCode.BadGateway,
HttpStatusCode.GatewayTimeout
};
var waitAndRetryPolicy = Policy
.HandleResult<HttpResponseMessage>(e => e.StatusCode == HttpStatusCode.ServiceUnavailable || e.StatusCode == (HttpStatusCode)429)
.WaitAndRetryAsync(10,
attempt => TimeSpan.FromSeconds(5), (exception, calculatedWaitDuration) =>
{
_log.Info($"Bitfinex API server is throttling our requests. Automatically delaying for {calculatedWaitDuration.TotalMilliseconds}ms");
}
);
var circuitBreakerPolicyForRecoverable = Policy
.Handle<HttpResponseException>()
.OrResult<HttpResponseMessage>(r => retryCodes.Contains(r.StatusCode))
.CircuitBreakerAsync(
handledEventsAllowedBeforeBreaking: 3,
durationOfBreak: TimeSpan.FromSeconds(3),
onBreak: (outcome, breakDelay) =>
{
_log.Info($"Polly Circuit Breaker logging: Breaking the circuit for {breakDelay.TotalMilliseconds}ms due to: {outcome.Exception?.Message ?? …Run Code Online (Sandbox Code Playgroud) 我有一个 Azure 函数,可以对 webapi 端点进行 http 调用。我正在关注此示例GitHub Polly RetryPolicy,因此我的代码具有类似的结构。所以在 Startup.cs 中我有:
builder.Services.AddPollyPolicies(config); // extension methods setting up Polly retry policies
builder.Services.AddHttpClient("MySender", client =>
{
client.BaseAddress = config.SenderUrl;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
});
Run Code Online (Sandbox Code Playgroud)
我的重试策略如下所示:
public static class PollyRegistryExtensions
{
public static IPolicyRegistry<string> AddBasicRetryPolicy(this IPolicyRegistry<string> policyRegistry, IMyConfig config)
{
var retryPolicy = Policy
.Handle<Exception>()
.OrResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
.WaitAndRetryAsync(config.ServiceRetryAttempts, retryCount => TimeSpan.FromMilliseconds(config.ServiceRetryBackOffMilliSeconds), (result, timeSpan, retryCount, context) =>
{
if (!context.TryGetLogger(out var logger)) return;
logger.LogWarning(
$"Service delivery attempt {retryCount} failed, next attempt in {timeSpan.TotalMilliseconds} …Run Code Online (Sandbox Code Playgroud) 有没有一种优雅的方法来做到这一点?我在特定时间传递了太多请求,从而引发了 503(服务不可用)异常。谢谢
protected void CallApi(string uriString)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsync(uriString, new StringContent("{ Data to be posted }")).Result;
for (int i = 0; i < MaxRetries; i++)
{
if (response.IsSuccessStatusCode)
{
break;
}
else
{
Thread.Sleep(TimeSpan.FromMinutes(1));
response = client.PostAsync(uriString, new StringContent("{ Data to be posted }")).Result;
}
}
throw new Exception("status : " + (int)response.StatusCode + ", Content :" + response.Content.ReadAsStringAsync().Result);
}
}
Run Code Online (Sandbox Code Playgroud) c# ×6
polly ×3
.net ×1
.net-core ×1
asp.net ×1
httprequest ×1
httpresponse ×1
policy ×1
retrypolicy ×1