0

对于同一请求的多次重试,我收到错误“请求消息已发送。无法多次发送相同的请求消息”。因此,据我所知,我需要重新创建一个新的 HttpRequestMessage 以进行第二次重试。有没有办法知道 retryPolicy.Execute() 中的重试迭代次数,以便我可以克隆 HttpRequestMessage 以进行第二次重试空?

    using Polly; 
    using Polly.Retry;

    namespace RetryLogic {
        public class HttpFactory
        {
            private static readonly TimeSpan defaultTimeoutInMinutes = TimeSpan.FromMinutes(5);
            private static readonly RetryPolicy<HttpResponseMessage> retryPolicy;
            private static readonly HttpClient client;

            static HttpFactory()
            {
                retryPolicy = Policy
                    .Handle<Exception>()
                    .OrResult<HttpResponseMessage>(resp => resp.StatusCode == HttpStatusCode.GatewayTimeout
                                                           || resp.StatusCode == HttpStatusCode.ServiceUnavailable
                                                           || resp.StatusCode == (HttpStatusCode)550)
                    .WaitAndRetry(1, GetRetryDelay);
                var handler = new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };
                client = new HttpClient(handler)
                {
                    Timeout = defaultTimeoutInMinutes
                };
            }

            public static HttpRequestMessage CreateRequest(HttpMethod method, string pathAndQuery, NameValueCollection headers, Stream requestBody)
            {
                HttpRequestMessage request = new HttpRequestMessage(method, pathAndQuery);
                if (requestBody != null)
                {
                    request.Content = new StreamContent(requestBody);
                    request.Headers.TransferEncodingChunked = true;
                }
                return AddRequestHeaders(request, headers);
            }

            public static HttpResponseMessage MakeRequest(HttpRequestMessage request, ILoggerFactory factory, List<HttpOptions> httpOptions = null)
            {
                return retryPolicy.Execute(() =>
                {
// How to know the retry iteration count here?
                    HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead;
                    if (httpOptions != null)
                    {
                        foreach (HttpOptions httpOption in httpOptions)
                        {
                            switch (httpOption)
                            {
                                case HttpOptions.SkipReadingResponseContent:
                                    completionOption = HttpCompletionOption.ResponseHeadersRead;
                                    break;
                                case HttpOptions.TransferChunkEncodedRequest:
                                    client.DefaultRequestHeaders.TransferEncodingChunked = true;
                                    break;
                            }
                        }
                    }

                    HttpResponseMessage response = client.SendAsync(request, completionOption).Result;


                    return response;
                });
            }

        }

        public enum HttpOptions
        {
            SkipReadingResponseContent,
            TransferChunkEncodedRequest
        } }
4

1 回答 1

0

Retry Polly 不提供任何内置功能来了解迭代次数。

int iterationNumber = 0;
policy.Execute(() => { 
   iterationNumber++;
   //  ... 
} );
于 2018-04-24T02:42:45.790 回答