diff --git a/docs/fundamentals/networking/http/httpclient-guidelines.md b/docs/fundamentals/networking/http/httpclient-guidelines.md index deff28846f374..87066001968c6 100644 --- a/docs/fundamentals/networking/http/httpclient-guidelines.md +++ b/docs/fundamentals/networking/http/httpclient-guidelines.md @@ -52,6 +52,38 @@ To summarize recommended `HttpClient` use in terms of lifetime management, you s For more information about managing `HttpClient` lifetime with `IHttpClientFactory`, see [`IHttpClientFactory` guidelines](../../../core/extensions/httpclient-factory.md#httpclient-lifetime-management). +## Resilience policies with static clients + +It's possible to configure a `static` or *singleton* client to use any number of resilience policies using the following pattern: + +```csharp +using System; +using System.Net.Http; +using Microsoft.Extensions.Http; +using Polly; +using Polly.Extensions.Http; + +var retryPolicy = HttpPolicyExtensions + .HandleTransientHttpError() + .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); + +var socketHandler = new SocketsHttpHandler { PooledConnectionLifetime = TimeSpan.FromMinutes(15) }; +var pollyHandler = new PolicyHttpMessageHandler(retryPolicy) +{ + InnerHandler = socketHandler; +}; + +var httpClient = new HttpClient(pollyHandler); +``` + +The preceding code: + +- Relies on [Microsoft.Extensions.Http.Polly](https://www.nuget.org/packages/Microsoft.Extensions.Http.Polly) NuGet package, transitively the [Polly.Extensions.Http](https://www.nuget.org/packages/Polly.Extensions.Http) NuGet package for the `HttpPolicyExtensions` type. +- Specifies a transient HTTP error handler, configured with retry policy that with each attempt will exponentially backoff delay intervals. +- Defines a pooled connection lifetime of fifteen minutes for the `socketHandler`. +- Passes the `socketHandler` to the `policyHandler` with the retry logic. +- Instantiates an `HttpClient` given the `policyHandler`. + ## See also - [HTTP support in .NET](http-overview.md)