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

[Resilient HTTP apps] Add a paragraph describing Http.Resilience RemoveAllResilienceHandlers method #45335

Merged
merged 11 commits into from
Mar 17, 2025
15 changes: 14 additions & 1 deletion docs/core/resilience/http-resilience.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
There are several resilience-centric extensions available. Some are standard, thus employing various industry best practices, and others are more customizable. When adding resilience, you should only add one resilience handler and avoid stacking handlers. If you need to add multiple resilience handlers, you should consider using the `AddResilienceHandler` extension method, which allows you to customize the resilience strategies.

> [!IMPORTANT]
> All of the examples within this article rely on the <xref:Microsoft.Extensions.DependencyInjection.HttpClientFactoryServiceCollectionExtensions.AddHttpClient%2A> API, from the [Microsoft.Extensions.Http](https://www.nuget.org/packages/Microsoft.Extensions.Http) library, which returns an <xref:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder> instance. The <xref:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder> instance is used to configure the <xref:System.Net.Http.HttpClient> and add the resilience handler.
> All the examples within this article rely on the <xref:Microsoft.Extensions.DependencyInjection.HttpClientFactoryServiceCollectionExtensions.AddHttpClient%2A> API, from the [Microsoft.Extensions.Http](https://www.nuget.org/packages/Microsoft.Extensions.Http) library, which returns an <xref:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder> instance. The <xref:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder> instance is used to configure the <xref:System.Net.Http.HttpClient> and add the resilience handler.

## Add standard resilience handler

Expand Down Expand Up @@ -77,6 +77,19 @@

The preceding code adds the standard resilience handler to the <xref:System.Net.Http.HttpClient>. Like most resilience APIs, there are overloads that allow you to customize the default options and applied resilience strategies.

## Remove standard resilience handler

The following example demonstrates how to configure a custom <xref:System.Net.Http.HttpClient> using the `AddHttpClient` method, remove all predefined resilience strategies, and add new custom ones.

Check failure on line 82 in docs/core/resilience/http-resilience.md

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces

docs/core/resilience/http-resilience.md:82:199 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md009.md
This approach allows you to clear existing configurations and define new ones according to your specific requirements.
:::code language="csharp" source="snippets/http-resilience/Program.RemoveHandlers.cs" id="remove-handlers":::

The preceding code:

- Adds the standard resilience handler to the named <xref:System.Net.Http.HttpClient> instance
- Removes all predefined resilience handlers that were previously registered. This is useful when you want to start with a clean slate and add your own custom strategies.
- Adds a `StandardHedgingHandler` to the <xref:System.Net.Http.HttpClient>.You can replace `AddStandardHedgingHandler()` with any strategy that suits your application's needs, such as retry mechanisms, circuit breakers, or other resilience techniques.


Check failure on line 92 in docs/core/resilience/http-resilience.md

View workflow job for this annotation

GitHub Actions / lint

Multiple consecutive blank lines

docs/core/resilience/http-resilience.md:92 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2] https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md012.md
### Standard resilience handler defaults

The default configuration chains five resilience strategies in the following order (from the outermost to the innermost):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http.Resilience;
using Polly;

internal partial class Program
{
private static void RemoveHandlers(IHttpClientBuilder httpClientBuilder)
{
// <remove-handlers>
services.ConfigureHttpClientDefaults(builder => builder.AddStandardResilienceHandler());

Check failure on line 11 in docs/core/resilience/snippets/http-resilience/Program.RemoveHandlers.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\core\resilience\snippets\http-resilience\Program.RemoveHandlers.cs(11,9): error CS0103: The name 'services' does not exist in the current context [D:\a\docs\docs\docs\core\resilience\snippets\http-resilience\http-resilience.csproj]
// For a named HttpClient "custom" we want to remove the StandardResilienceHandler and add the StandardHedgingHandler instead.
services.AddHttpClient("custom")

Check failure on line 13 in docs/core/resilience/snippets/http-resilience/Program.RemoveHandlers.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\core\resilience\snippets\http-resilience\Program.RemoveHandlers.cs(13,9): error CS0103: The name 'services' does not exist in the current context [D:\a\docs\docs\docs\core\resilience\snippets\http-resilience\http-resilience.csproj]
.RemoveAllResilienceHandlers()
.AddStandardHedgingHandler();
// </remove-handlers>
}
}
Loading