5.9Configuring Retry Strategy

Updated on Aug 20, 2026~7 min read

Configures the retry strategy for a single request. By default, once a retry strategy is configured, the retry mechanism is triggered automatically when an unsuppressed exception occurs during the request.

HTTP Declarative Requests configure the retry strategy through the RetryAttribute attribute. The corresponding HTTP declarative extractor is implemented by the RetryDeclarativeExtractor type, which is responsible for parsing the RetryAttribute attribute and building the retry strategy configuration required for the HttpRequestBuilder instance.

cs
// Applied on the interface definition, affects all methods[Retry(3)]  // Max 3 retriespublic interface IHttpService : IHttpDeclarative{    [Get("https://furion.net/")]    Task<string> GetStringAsync();    // Applied on the method    [Retry(10)]   // Max 10 retries    [Get("https://furion.net/")]    Task<string> GetStringAsync();    [Retry(10, 1000)]   // Configure the retry interval    [Get("https://furion.net/")]    Task<string> GetStringAsync();    [Retry(10, RetryStatusCodes = [401])]   // Retry on specific HTTP status codes    [Get("https://furion.net/")]    Task<string> GetStringAsync();    [Retry(10, RetryExceptionTypes = [typeof(InvalidOperationException)])]   // Retry on specific exception types    [Get("https://furion.net/")]    Task<string> GetStringAsync();    [Retry(RetryIndefinitely = true)]   // Set unlimited retries until success    [Get("https://furion.net/")]    Task<string> GetStringAsync();}

RetryAttribute contains the following constructors and properties:

  • Constructors:

    • new(maxRetries): applies to a method or interface, sets the maximum number of retries (0 means no retries).
    • new(maxRetries, retryInterval): applies to a method or interface, sets the maximum number of retries (0 means no retries) and the base retry interval (in milliseconds).
  • Properties:

    • MaxRetries: the maximum number of retries (int type). The default value is 0, meaning no retries. If RetryIntervals is set, this value is automatically overridden to the array length.
    • RetryInterval: the base retry interval (in milliseconds) (double type). The default value is 1000 milliseconds. Only takes effect when RetryIntervals is not set.
    • UseExponentialBackoff: whether to use exponential backoff for retries (bool type). The default value is false. When set to true, each retry interval = RetryInterval * 2^(retry-1). Only takes effect when RetryIntervals is not set.
    • RetryIntervals: a custom array of retry intervals (in milliseconds) (double[]? 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: the set of HTTP status codes to retry (int[]? type). If empty, only failures caused by exceptions are retried.
    • RetryExceptionTypes: the set of exception types to retry (Type[]? type). If empty, all Exception types are retried (subject to MaxRetries).
    • RetryIndefinitely: whether to retry indefinitely until success (bool type). The default value is false. When set to true, MaxRetries and the length of RetryIntervals are ignored, and retries continue until success or a non-retryable exception occurs.