PolicyHttpMessageHandler 类


DelegatingHandler 实现,执行由 Policy 包围的请求处理。A DelegatingHandler implementation that executes request processing surrounded by a Policy.

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


此消息处理程序实现支持使用由 Polly 库提供的策略来实现暂时性故障处理和复原。This message handler implementation supports the use of policies provided by the Polly library for transient-fault-handling and resiliency.

此处提供的文档是将 Polly 与结合使用的重点指导 IHttpClientFactoryThe documentation provided here is focused guidance for using Polly together with the IHttpClientFactory. 有关 Polly 的权威信息,请参阅 Polly 项目及其 相关文档See the Polly project and its documentation for authoritative information on Polly.

上的扩展方法 PollyHttpClientBuilderExtensions 旨在用作一种方便且正确的方法来创建 PolicyHttpMessageHandlerThe extension methods on PollyHttpClientBuilderExtensions are designed as a convenient and correct way to create a PolicyHttpMessageHandler.

AddPolicyHandler(IHttpClientBuilder, IAsyncPolicy<HttpResponseMessage>)方法支持 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并且 PollyHttpClientBuilderExtensions 便利方法仅接受泛型 IAsyncPolicy`1PolicyHttpMessageHandler and the PollyHttpClientBuilderExtensions convenience methods only accept the generic IAsyncPolicy`1. 可以通过使用上的泛型方法来创建泛型策略实例, PolicyTimeoutAsync``1(System.Int32)Generic policy instances can be created by using the generic methods on Policy such as TimeoutAsync``1(System.Int32).

若要改编现有的非泛型 IAsyncPolicy ,请使用如下所示的代码:To adapt an existing non-generic IAsyncPolicy, use code like the following:


AddTransientHttpErrorPolicy(IHttpClientBuilder, Func<PolicyBuilder<HttpResponseMessage>,IAsyncPolicy<HttpResponseMessage>>) 方法是一种固执的简便方法,该方法支持应用程序的策略,该方法因连接失败或服务器错误 (5XX HTTP 状态代码) 而失败。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). 此类方法仅支持重试、Circuit-Breaker 或回退等反应策略。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.

使用诸如重试或超时等策略时要小心,因为 HttpClient 通过提供自己的超时 TimeoutTake care when using policies such as Retry or Timeout together, as HttpClient provides its own timeout via Timeout. 合并重试和超时时, Timeout 将在所有尝试中充当超时; 可在配置序列中的重试策略后配置 Polly 超时策略,以提供每次尝试超时。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.

Polly 提供的所有策略都设计为在长期使用时有效。All policies provided by Polly are designed to be efficient when used in a long-lived way. 某些策略(如 Bulkhead 和 Circuit-Breaker 维护状态,并且应在要共享 Bulkhead 或 Circuit-Breaker 状态的调用范围内。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. 提供的扩展方法 PollyHttpClientBuilderExtensions 旨在为策略分配长生存期,并确保在处理程序轮换功能处于活动状态时可以使用它们。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.

PolicyHttpMessageHandler HttpRequestMessage 如果不存在,将在执行之前将上下文附加到 PolicyThe PolicyHttpMessageHandler will attach a context to the HttpRequestMessage prior to executing a Policy, if one does not already exist. Context将向策略提供,以供在 Policy 和其他消息处理程序中使用。The Context will be provided to the policy for use inside the Policy and in other message handlers.



创建一个新的 PolicyHttpMessageHandlerCreates a new PolicyHttpMessageHandler.


创建一个新的 PolicyHttpMessageHandlerCreates a new PolicyHttpMessageHandler.



获取或设置处理 HTTP 响应消息的内部处理程序。Gets or sets the inner handler which processes the HTTP response messages.

(继承自 DelegatingHandler)



释放由 HttpMessageHandler 使用的非托管资源和托管资源。Releases the unmanaged resources and disposes of the managed resources used by the HttpMessageHandler.

(继承自 HttpMessageHandler)

释放由 DelegatingHandler 使用的非托管资源,并可根据需要释放托管资源。Releases the unmanaged resources used by the DelegatingHandler, and optionally disposes of the managed resources.

(继承自 DelegatingHandler)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Send(HttpRequestMessage, CancellationToken)

将 HTTP 请求发送到内部处理程序,然后再发送到服务器。Sends an HTTP request to the inner handler to send to the server.

(继承自 DelegatingHandler)
SendAsync(HttpRequestMessage, CancellationToken)

以异步操作发送 HTTP 请求。Send an HTTP request as an asynchronous operation.

SendCoreAsync(HttpRequestMessage, Context, CancellationToken)

已在 Policy 的执行内部调用,以执行请求处理。Called inside the execution of the Policy to perform request processing.


返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)