DynamicHttpErrorHandler Class
- Namespace
- Kampute.HttpClient.ErrorHandlers
- Assembly
- Kampute.HttpClient.dll
Definition
Handles error responses with a function, without defining a handler class.
public class DynamicHttpErrorHandler : IHttpErrorHandler- Inheritance
- object
- DynamicHttpErrorHandler
- Implements
Remarks
The handler accepts every status code, so it is asked about every error response that the handlers added before it do not retry. The function decides, for example by checking HttpResponseErrorContext.Response, whether to return a request to retry.
Examples
This handler retries a request up to three times, one second apart, while the server answers '409 Conflict'. The policy object is the source of the retries, so they are counted separately from the retries of other handlers.
var conflictRetries = RetryStrategies.Constant(TimeSpan.FromSeconds(1)).WithMaxRetries(3).ToHttpRetryPolicy();
client.ErrorHandlers.Add(new DynamicHttpErrorHandler((ctx, cancellationToken) =>
ctx.Response.StatusCode == HttpStatusCode.Conflict
? ctx.ScheduleRetryAsync(conflictRetries, errorContext => conflictRetries.CreateSession(errorContext), cancellationToken)
: Task.FromResult(HttpErrorHandlerResult.NoRetry)));Constructors
DynamicHttpErrorHandler(Func<HttpResponseErrorContext, CancellationToken, Task<HttpErrorHandlerResult>>)
Initializes a new instance of the DynamicHttpErrorHandler class.
public DynamicHttpErrorHandler(Func<HttpResponseErrorContext, CancellationToken, Task<HttpErrorHandlerResult>> asyncHandler)Parameters
asyncHandlerFunc<HttpResponseErrorContext, CancellationToken, Task<HttpErrorHandlerResult>>- The function that decides. It receives the context of the error response and a cancellation token, and returns the decision.
Exceptions
- ArgumentNullException
- Thrown if
asyncHandlerisnull.
Methods
CanHandle(HttpStatusCode)
Determines whether the handler is capable of handling the provided HTTP status code.
public bool CanHandle(HttpStatusCode statusCode)Parameters
statusCodeHttpStatusCode- The HTTP status code to evaluate.
Returns
DecideOnRetryAsync(HttpResponseErrorContext, CancellationToken)
Calls the function of this handler to decide whether to retry a request after an error response.
protected virtual Task<HttpErrorHandlerResult> DecideOnRetryAsync(HttpResponseErrorContext ctx, CancellationToken cancellationToken)Parameters
ctxHttpResponseErrorContext- The context containing information about the HTTP response that indicates a failure.
cancellationTokenCancellationToken- A token for canceling the operation.
Returns
- Task<HttpErrorHandlerResult>
- A task that resolves to an HttpErrorHandlerResult, indicating whether the request should be retried.
Explicit Interface Implementations
IHttpErrorHandler.DecideOnRetryAsync(HttpResponseErrorContext, CancellationToken)
Decides whether to retry a request after an error response.
Task<HttpErrorHandlerResult> IHttpErrorHandler.DecideOnRetryAsync(HttpResponseErrorContext ctx, CancellationToken cancellationToken)Parameters
ctxHttpResponseErrorContext- The context containing information about the HTTP response that indicates a failure.
cancellationTokenCancellationToken- A token for canceling the operation.
Returns
- Task<HttpErrorHandlerResult>
- A task that resolves to HttpErrorHandlerResult.Retry(HttpRequestMessage) with the request to send, or to HttpErrorHandlerResult.NoRetry to let the next handler decide.
Exceptions
- ArgumentNullException
- Thrown if
ctxisnull.
