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.
// 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 (0means no retries).new(maxRetries, retryInterval): applies to a method or interface, sets the maximum number of retries (0means no retries) and the base retry interval (in milliseconds).
-
Properties:
MaxRetries: the maximum number of retries (inttype). The default value is0, meaning no retries. IfRetryIntervalsis set, this value is automatically overridden to the array length.RetryInterval: the base retry interval (in milliseconds) (doubletype). The default value is1000milliseconds. Only takes effect whenRetryIntervalsis not set.UseExponentialBackoff: whether to use exponential backoff for retries (booltype). The default value isfalse. When set totrue, each retry interval =RetryInterval * 2^(retry-1). Only takes effect whenRetryIntervalsis 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, andMaxRetriesandUseExponentialBackoffare ignored. Each retry uses the interval at the corresponding index in the array, in order.RetryStatusCodes: the set ofHTTPstatus 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, allExceptiontypes are retried (subject toMaxRetries).RetryIndefinitely: whether to retry indefinitely until success (booltype). The default value isfalse. When set totrue,MaxRetriesand the length ofRetryIntervalsare ignored, and retries continue until success or a non-retryable exception occurs.