PolicyHttpMessageHandler PolicyHttpMessageHandler PolicyHttpMessageHandler Class

Definition

A DelegatingHandler implementation that executes request processing surrounded by a Polly.Policy.

public class PolicyHttpMessageHandler : System.Net.Http.DelegatingHandler
type PolicyHttpMessageHandler = class
    inherit DelegatingHandler
Public Class PolicyHttpMessageHandler
Inherits DelegatingHandler
Inheritance
PolicyHttpMessageHandlerPolicyHttpMessageHandlerPolicyHttpMessageHandler

Remarks

This message handler implementation supports the use of policies provided by the Polly library for transient-fault-handling and resiliency.

The documentation provided here is focused guidance for using Polly together with the IHttpClientFactory. See the Polly project and its documentation (https://github.com/app-vnext/Polly) for authoritative information on Polly.

The extension methods on PollyHttpClientBuilderExtensions are designed as a convenient and correct way to create a PolicyHttpMessageHandler.

The AddPolicyHandler(IHttpClientBuilder, IAsyncPolicy<HttpResponseMessage>) method supports the creation of a PolicyHttpMessageHandler for any kind of policy. This includes non-reactive policies, such as Timeout or Cache, which don't require the underlying request to fail first.

PolicyHttpMessageHandler and the PollyHttpClientBuilderExtensions convenience methods only accept the generic Polly.IAsyncPolicy. Generic policy instances can be created by using the generic methods on Polly.Policy such as Polly.Policy.TimeoutAsync``1(System.Int32).

To adapt an existing non-generic Polly.IAsyncPolicy, use code like the following: Converting a non-generic

IAsyncPolicy policy
to Polly.IAsyncPolicy.

      policy.AsAsyncPolicy<HttpResponseMessage>()

The AddTransientHttpErrorPolicy(IHttpClientBuilder, Func<PolicyBuilder<HttpResponseMessage>,IAsyncPolicy<HttpResponseMessage>>) method is an opinionated convenience method that supports the application of a policy for requests that fail due to a connection failure or server error (5XX HTTP status code). This kind of method supports only reactive policies such as Retry, Circuit-Breaker or Fallback. This method is only provided for convenience; we recommend creating your own policies as needed if this does not meet your requirements.

Take care when using policies such as Retry or Timeout together as HttpClient provides its own timeout via Timeout. When combining Retry and Timeout, Timeout will act as a timeout across all tries; a Polly Timeout policy can be configured after a Retry policy in the configuration sequence, to provide a timeout-per-try.

All policies provided by Polly are designed to be efficient when used in a long-lived way. Certain policies such as the Bulkhead and Circuit-Breaker maintain state and should be scoped across calls you wish to share the Bulkhead or Circuit-Breaker state. Take care to ensure the correct lifetimes when using policies and message handlers together in custom scenarios. The extension methods provided by PollyHttpClientBuilderExtensions are designed to assign a long lifetime to policies and ensure that they can be used when the handler rotation feature is active.

The PolicyHttpMessageHandler will attach a context to the HttpRequestMessage prior to executing a Polly.Policy, if one does not already exist. The Polly.Context will be provided to the policy for use inside the Polly.Policy and in other message handlers.

Constructors

PolicyHttpMessageHandler(IAsyncPolicy<HttpResponseMessage>) PolicyHttpMessageHandler(IAsyncPolicy<HttpResponseMessage>) PolicyHttpMessageHandler(IAsyncPolicy<HttpResponseMessage>)

Creates a new PolicyHttpMessageHandler.

PolicyHttpMessageHandler(Func<HttpRequestMessage,IAsyncPolicy<HttpResponseMessage>>) PolicyHttpMessageHandler(Func<HttpRequestMessage,IAsyncPolicy<HttpResponseMessage>>) PolicyHttpMessageHandler(Func<HttpRequestMessage,IAsyncPolicy<HttpResponseMessage>>)

Creates a new PolicyHttpMessageHandler.

Methods

SendAsync(HttpRequestMessage, CancellationToken) SendAsync(HttpRequestMessage, CancellationToken) SendAsync(HttpRequestMessage, CancellationToken)
SendCoreAsync(HttpRequestMessage, Context, CancellationToken) SendCoreAsync(HttpRequestMessage, Context, CancellationToken) SendCoreAsync(HttpRequestMessage, Context, CancellationToken)

Called inside the execution of the Polly.Policy to perform request processing.

Applies to