HttpRetryPolicy Class
- Namespace
- Kampute.HttpClient
- Assembly
- Kampute.HttpClient.dll
Definition
Retries failed HTTP requests as a retry strategy decides.
public class HttpRetryPolicy : IHttpRetryPolicy- Inheritance
- object
- HttpRetryPolicy
- Implements
Remarks
Examples
This policy retries a request up to five times after connection failures, with delays that grow with the Fibonacci sequence from one second:
client.RetryPolicy = RetryStrategies.Fibonacci(TimeSpan.FromSeconds(1)).WithMaxRetries(5).ToHttpRetryPolicy();Constructors
HttpRetryPolicy(IRetryStrategy)
Initializes a new instance of the HttpRetryPolicy class with the specified retry strategy.
public HttpRetryPolicy(IRetryStrategy strategy)Parameters
strategyIRetryStrategy- The retry strategy that decides whether and when a failed request is retried.
Exceptions
- ArgumentNullException
- Thrown if
strategyisnull.
Properties
None
Gets a policy that never retries.
public static HttpRetryPolicy None { get; }Property Value
- HttpRetryPolicy
- A policy based on RetryStrategies.None.
Strategy
Gets the retry strategy of this policy.
public virtual IRetryStrategy Strategy { get; }Property Value
- IRetryStrategy
- The IRetryStrategy that decides whether and when a failed request is retried.
Methods
CreateSession(HttpRequestErrorContext)
Creates a retry session that applies the strategy of this policy to a failed request.
public virtual IRetrySession CreateSession(HttpRequestErrorContext ctx)Parameters
ctxHttpRequestErrorContext- The context of the failure.
Returns
- IRetrySession
- A new RetrySession for Strategy.
Exceptions
- ArgumentNullException
- Thrown if
ctxisnull.
Dynamic(Func<HttpRequestErrorContext, IRetryStrategy>)
Creates a policy that chooses the retry strategy for each failed request.
public static IHttpRetryPolicy Dynamic(Func<HttpRequestErrorContext, IRetryStrategy> strategyFactory)Parameters
strategyFactoryFunc<HttpRequestErrorContext, IRetryStrategy>- A function that returns the retry strategy for the context of a failure.
Returns
- IHttpRetryPolicy
- A policy that starts a session with the strategy that
strategyFactoryreturns.
Exceptions
- ArgumentNullException
- Thrown if
strategyFactoryisnull.
Remarks
Dynamic(Func<HttpRequestErrorContext, IRetrySession>)
Creates a policy that creates the retry session for each failed request.
public static IHttpRetryPolicy Dynamic(Func<HttpRequestErrorContext, IRetrySession> sessionFactory)Parameters
sessionFactoryFunc<HttpRequestErrorContext, IRetrySession>- A function that returns the retry session for the context of a failure.
Returns
- IHttpRetryPolicy
- A policy that uses the session that
sessionFactoryreturns.
Exceptions
- ArgumentNullException
- Thrown if
sessionFactoryisnull.

Create a policy from any IRetryStrategy with ToHttpRetryPolicy(this IRetryStrategy). Each call that fails gets its own RetrySession, so the strategy, and the policy, can be shared by any number of requests and clients.
To choose the strategy or the session from the failure, use Dynamic(Func<HttpRequestErrorContext, IRetryStrategy>) or Dynamic(Func<HttpRequestErrorContext, IRetrySession>).