Skip to content

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

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

Merged
merged 11 commits into from
Mar 17, 2025
18 changes: 17 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 @@ To add resilience to an <xref:System.Net.Http.HttpClient>, you chain a call on t
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,22 @@ Given that you've created an <xref:Microsoft.Extensions.DependencyInjection.IHtt

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 handlers

We provide a method xref:RemoveAllResilienceHandlers which removes all previously registered resilience handlers. It is useful when you need to clear all existing resilience handlers and add your custom one.

The following example demonstrates how to configure a custom <xref:System.Net.Http.HttpClient> using the `AddHttpClient` method, remove all predefined resilience strategies, and replace them with new handlers.
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:

- Creates a <xref:Microsoft.Extensions.DependencyInjection.ServiceCollection> instance.
- 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 state 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.

### 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,20 @@
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>
var services = new ServiceCollection();
services.ConfigureHttpClientDefaults(builder => builder.AddStandardResilienceHandler());
// For a named HttpClient "custom" we want to remove the StandardResilienceHandler and add the StandardHedgingHandler instead.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Resilience", "EXTEXP0001:Experimental API")]

Check failure on line 14 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(14,3): error CS7014: Attributes are not valid in this context. [D:\a\docs\docs\docs\core\resilience\snippets\http-resilience\http-resilience.csproj]
services.AddHttpClient("custom")

Check failure on line 15 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(15,3): error EXTEXP0001: 'Microsoft.Extensions.DependencyInjection.ResilienceHttpClientBuilderExtensions.RemoveAllResilienceHandlers(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder)' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. (https://aka.ms/dotnet-extensions-warnings/EXTEXP0001) [D:\a\docs\docs\docs\core\resilience\snippets\http-resilience\http-resilience.csproj]
.RemoveAllResilienceHandlers()
.AddStandardHedgingHandler();
// </remove-handlers>
}
}
Loading