Ejemplo n.º 1
0
        public RetryingHandler(TimeSpan timeout, TimeSpan uploadTimeout, int maxRetries, Func <int, DelegateResult <HttpResponseMessage>, Context, TimeSpan> sleepDurationProvider)
        {
            AsyncRetryPolicy <HttpResponseMessage> retryAfterPolicy = Policy
                                                                      .HandleResult <HttpResponseMessage>(ContainsRetryAfterHeader)
                                                                      .WaitAndRetryAsync(maxRetries, sleepDurationProvider, (outcome, timespan, retryCount, context) => Task.CompletedTask);

            AsyncRetryPolicy <HttpResponseMessage> httpRetryPolicy =
                Policy
                .Handle <HttpRequestException>()
                .Or <TaskCanceledException>()
                .Or <TimeoutRejectedException>()
                .OrResult <HttpResponseMessage>(RetryRequired)
                .WaitAndRetryAsync(GetBackOff(maxRetries));

            AsyncPolicyWrap <HttpResponseMessage> baseRetryPolicy = retryAfterPolicy.WrapAsync(httpRetryPolicy);

            _policy           = baseRetryPolicy.WrapAsync(Policy.TimeoutAsync(timeout));
            _fileUploadPolicy = baseRetryPolicy.WrapAsync(Policy.TimeoutAsync(uploadTimeout));
        }