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.

cs
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 success

HttpRetryOptions contains the following properties and methods:

  • Properties:

    • MaxRetries: Maximum number of retries (int type). Defaults to 0, meaning no retry. If RetryIntervals is set, this value is automatically overridden with the array length.
    • RetryInterval: Base retry interval (TimeSpan type). Defaults to 1 second. Takes effect only when RetryIntervals is not set.
    • UseExponentialBackoff: Whether to use exponential backoff for retries (bool type). Defaults to false. When set to true, each retry interval = RetryInterval * 2^(retry-1). Takes effect only when RetryIntervals is not set.
    • RetryIntervals: Custom retry interval array (IList<TimeSpan>? type). If this property is set, the number of retries equals the array length, and MaxRetries and UseExponentialBackoff are ignored. Each retry uses the interval at the corresponding index in the array, in order.
    • RetryStatusCodes: Collection of HTTP status 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, all Exceptions are retried (subject to MaxRetries).
    • 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 is HttpRetryContext, which contains the following properties:
      • Attempt: Current retry count (int type). Starts from 1.
      • Exception: The exception that triggered the retry (Exception? type).
      • StatusCode: The HTTP status code that triggered the retry (HttpStatusCode? type).
      • MaxRetries: Maximum number of retries (int type). -1 means unlimited (displayed as in logs).
      • IsExceptionRetry: Whether the retry was triggered by an exception (bool type).
      • IsStatusCodeRetry: Whether the retry was triggered by a status code (bool type).
    • RetryIndefinitely: Whether to retry indefinitely until success (bool type). Defaults to false. When set to true, MaxRetries and the length of RetryIntervals are 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) and SetRetryInterval(milliseconds): Sets the base retry interval.
    • SetUseExponentialBackoff(use): Sets whether to use exponential backoff.
    • SetRetryIntervals(intervals) and SetRetryIntervals(milliseconds): Sets the custom retry interval array.
    • AddRetryStatusCode(statusCode): Adds an HTTP status code to retry.
    • AddRetryStatusCodes(statusCodes): Adds multiple HTTP status codes to retry.
    • AddRetryException<TExcetion>() and AddRetryException(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.