Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Update the section on resilience and static clients" #39073

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions docs/fundamentals/networking/http/httpclient-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,37 @@ 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 with static clients
## Resilience policies with static clients

It's possible to configure a `static` or *singleton* client to use any number of resilience pipelines using the following pattern:
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 Microsoft.Extensions.Http.Resilience;
using Polly;
using Polly.Extensions.Http;

var retryPipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddRetry(new HttpRetryStrategyOptions
{
BackoffType = DelayBackoffType.Exponential,
MaxRetryAttempts = 3
})
.Build();
var retryPolicy = HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));

var socketHandler = new SocketsHttpHandler { PooledConnectionLifetime = TimeSpan.FromMinutes(15) };
var resilienceHandler = new ResilienceHandler(retryPipeline)
var pollyHandler = new PolicyHttpMessageHandler(retryPolicy)
{
InnerHandler = socketHandler,
};

var httpClient = new HttpClient(resilienceHandler);
var httpClient = new HttpClient(pollyHandler);
```

The preceding code:

- 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.
- 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 `resilienceHandler` with the retry logic.
- Instantiates an `HttpClient` given the `resilienceHandler`.
- Passes the `socketHandler` to the `policyHandler` with the retry logic.
- Instantiates an `HttpClient` given the `policyHandler`.

## See also

Expand Down
Loading