3.23Configuring Retry Policies
Created on Aug 17, 2026~9 min read
Configures the retry policy for a single request. By default, if a retry policy is configured, the retry mechanism is triggered automatically when an unsuppressed exception occurs during the request.
HttpRequestBuilder.Get("https://furion.net/") .SetRetry(3); // Maximum number of retries; 0 means no retryHttpRequestBuilder.Get("https://furion.net/") .SetRetry(3, ctx => Console.WriteLine($"This is attempt {ctx.Attempt}.")); // Configures the callback delegate invoked before each retryHttpRequestBuilder.Get("https://furion.net/") .SetRetry(3, TimeSpan.FromSeconds(1)); // Configures the retry intervalHttpRequestBuilder.Get("https://furion.net/") .SetRetry(3, TimeSpan.FromSeconds(1), ctx => Console.WriteLine($"This is attempt {ctx.Attempt}.")); // Configures the callback delegate invoked before each retryHttpRequestBuilder.Get("https://furion.net/") .SetRetry(3, 1000); // Configures the retry interval (milliseconds)HttpRequestBuilder.Get("https://furion.net/") .SetRetry(3, 1000, ctx => Console.WriteLine($"This is attempt {ctx.Attempt}.")); // Configures the callback delegate invoked before each retryHttpRequestBuilder.Get("https://furion.net/") .SetRetry(new HttpRetryOptions()); // Custom retry optionsHttpRequestBuilder.Get("https://furion.net/") .SetRetry(options => options.SetMaxRetries(3)); // Custom retry optionsHttpRequestBuilder.Get("https://furion.net/") .SetRetry(options => options.SetMaxRetries(3) .AddRetryStatusCodes(401)); // Retries on specific HTTP status codesHttpRequestBuilder.Get("https://furion.net/") .SetRetry(options => options.SetMaxRetries(3) .AddRetryExceptions(typeof(InvalidOperationException))); // Retries on specific exception typesHttpRequestBuilder.Get("https://furion.net/") .SetRetryIndefinitely(); // Sets infinite retry until successHttpRetryOptions contains the following properties and methods:
-
Properties:
MaxRetries: Maximum number of retries (inttype). Defaults to0, meaning no retry. IfRetryIntervalsis set, this value is automatically overridden with the array length.RetryInterval: Base retry interval (TimeSpantype). Defaults to1second. Takes effect only whenRetryIntervalsis not set.UseExponentialBackoff: Whether to use exponential backoff for retries (booltype). Defaults tofalse. When set totrue, each retry interval =RetryInterval * 2^(retry-1). Takes effect only whenRetryIntervalsis not set.RetryIntervals: Custom retry interval array (IList<TimeSpan>?type). If this property is set, the number of retries equals the array length, andMaxRetriesandUseExponentialBackoffare ignored. Each retry uses the interval at the corresponding index in the array, in order.RetryStatusCodes: Collection ofHTTPstatus codes to retry (HashSet<HttpStatusCode>?type). If empty, only failures caused by exceptions are retried.RetryExceptionTypes: Collection of exception types to retry (HashSet<Type>?type). If empty, allExceptions are retried (subject toMaxRetries).OnRetry: Callback delegate invoked before each retry (Action<HttpRetryContext>?type). Can be used for logging, sending notifications, and so on. If this callback is not set, the framework automatically outputs a default retry warning log. Its parameter isHttpRetryContext, which contains the following properties:Attempt: Current retry count (inttype). Starts from1.Exception: The exception that triggered the retry (Exception?type).StatusCode: TheHTTPstatus code that triggered the retry (HttpStatusCode?type).MaxRetries: Maximum number of retries (inttype).-1means unlimited (displayed as∞in logs).IsExceptionRetry: Whether the retry was triggered by an exception (booltype).IsStatusCodeRetry: Whether the retry was triggered by a status code (booltype).
RetryIndefinitely: Whether to retry indefinitely until success (booltype). Defaults tofalse. When set totrue,MaxRetriesand the length ofRetryIntervalsare ignored, and it keeps retrying until success or until a non-retryable exception occurs.
-
Methods:
SetMaxRetries(maxRetries): Sets the maximum number of retries.SetRetryInterval(interval)andSetRetryInterval(milliseconds): Sets the base retry interval.SetUseExponentialBackoff(use): Sets whether to use exponential backoff.SetRetryIntervals(intervals)andSetRetryIntervals(milliseconds): Sets the custom retry interval array.AddRetryStatusCode(statusCode): Adds anHTTPstatus code to retry.AddRetryStatusCodes(statusCodes): Adds multipleHTTPstatus codes to retry.AddRetryException<TExcetion>()andAddRetryException(exceptionType): Adds an exception type to retry.AddRetryExceptions(exceptionTypes): Adds multiple exception types to retry.SetOnRetry(onRetry): Sets the callback delegate invoked before each retry.SetRetryIndefinitely(retryIndefinitely): Sets whether to retry indefinitely.