HttpRetryPolicy Class

Namespace
Kampute.HttpClient
Assembly
  • Kampute.HttpClient.dll

Definition

Retries failed HTTP requests as a retry strategy decides.
public class HttpRetryPolicy : IHttpRetryPolicy
Inheritance
Implements

Remarks

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>).

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

strategy IRetryStrategy
The retry strategy that decides whether and when a failed request is retried.

Exceptions

ArgumentNullException
Thrown if strategy is null.

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

ctx HttpRequestErrorContext
The context of the failure.

Returns

IRetrySession
A new RetrySession for Strategy.

Exceptions

ArgumentNullException
Thrown if ctx is null.

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

strategyFactory Func<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 strategyFactory returns.

Exceptions

ArgumentNullException
Thrown if strategyFactory is null.

Remarks

If strategyFactory returns null, creating the session throws InvalidOperationException.

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

sessionFactory Func<HttpRequestErrorContext, IRetrySession>
A function that returns the retry session for the context of a failure.

Returns

IHttpRetryPolicy
A policy that uses the session that sessionFactory returns.

Exceptions

ArgumentNullException
Thrown if sessionFactory is null.

Remarks

If sessionFactory returns null, creating the session throws InvalidOperationException.

See Also