From b2eb1a344ba728274eeae1122c180c69f01018f5 Mon Sep 17 00:00:00 2001 From: martintmk <103487740+martintmk@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:54:56 +0100 Subject: [PATCH 1/3] Update the section on resilience and static clients --- .../networking/http/httpclient-guidelines.md | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/docs/fundamentals/networking/http/httpclient-guidelines.md b/docs/fundamentals/networking/http/httpclient-guidelines.md index a3ace3cabe8a8..5a501d8965b15 100644 --- a/docs/fundamentals/networking/http/httpclient-guidelines.md +++ b/docs/fundamentals/networking/http/httpclient-guidelines.md @@ -52,37 +52,41 @@ 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 +## Resilience pipelines with static clients -It's possible to configure a `static` or *singleton* client to use any number of resilience policies using the following pattern: +It's possible to configure a `static` or *singleton* client to use any number of resilience pipelines using the following pattern: ```csharp using System; using System.Net.Http; using Microsoft.Extensions.Http; +using Microsoft.Extensions.Http.Resilience; using Polly; -using Polly.Extensions.Http; -var retryPolicy = HttpPolicyExtensions - .HandleTransientHttpError() - .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); +var retryPipeline = new ResiliencePipelineBuilder() + .AddRetry(new HttpRetryStrategyOptions + { + BackoffType = DelayBackoffType.Exponential, + MaxRetryAttempts = 3 + }) + .Build(); var socketHandler = new SocketsHttpHandler { PooledConnectionLifetime = TimeSpan.FromMinutes(15) }; -var pollyHandler = new PolicyHttpMessageHandler(retryPolicy) +var resilienceHandler = new ResilienceHandler(retryPolicy) { InnerHandler = socketHandler, }; -var httpClient = new HttpClient(pollyHandler); +var httpClient = new HttpClient(resilienceHandler); ``` 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. +- Relies on [Microsoft.Extensions.Http.Resilience](https://www.nuget.org/packages/Microsoft.Extensions.Http.Resilience) NuGet package. +- Specifies a transient HTTP error handler, configured with retry pipeline 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`. +- Passes the `socketHandler` to the `resilienceHandler` with the retry logic. +- Instantiates an `HttpClient` given the `resilienceHandler`. ## See also From b08149c1f9e952f923d27638849559560e0013c4 Mon Sep 17 00:00:00 2001 From: martintmk <103487740+martintmk@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:57:38 +0100 Subject: [PATCH 2/3] Update httpclient-guidelines.md --- docs/fundamentals/networking/http/httpclient-guidelines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/networking/http/httpclient-guidelines.md b/docs/fundamentals/networking/http/httpclient-guidelines.md index 5a501d8965b15..2bc962d26f86c 100644 --- a/docs/fundamentals/networking/http/httpclient-guidelines.md +++ b/docs/fundamentals/networking/http/httpclient-guidelines.md @@ -52,7 +52,7 @@ 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 pipelines with static clients +## Resilience with static clients It's possible to configure a `static` or *singleton* client to use any number of resilience pipelines using the following pattern: From bbaa3590205aa0411729d2b9785dc7aac360809f Mon Sep 17 00:00:00 2001 From: martintmk <103487740+martintmk@users.noreply.github.com> Date: Tue, 9 Jan 2024 21:40:21 +0100 Subject: [PATCH 3/3] Update httpclient-guidelines.md --- docs/fundamentals/networking/http/httpclient-guidelines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/networking/http/httpclient-guidelines.md b/docs/fundamentals/networking/http/httpclient-guidelines.md index 2bc962d26f86c..fd97fcc5b7d88 100644 --- a/docs/fundamentals/networking/http/httpclient-guidelines.md +++ b/docs/fundamentals/networking/http/httpclient-guidelines.md @@ -72,7 +72,7 @@ var retryPipeline = new ResiliencePipelineBuilder() .Build(); var socketHandler = new SocketsHttpHandler { PooledConnectionLifetime = TimeSpan.FromMinutes(15) }; -var resilienceHandler = new ResilienceHandler(retryPolicy) +var resilienceHandler = new ResilienceHandler(retryPipeline) { InnerHandler = socketHandler, };