Skip to content

Commit 37e086b

Browse files
[Resilient HTTP apps] Add a paragraph describing Http.Resilience RemoveAllResilienceHandlers method (#45335)
* Documentation for the Http.Resilience RemoveAllResilienceHandlers method * Fixing the build, resolving pr comments * Silencing the warning for the experimental API * PR review fixes * trying to pass the snippet build * Include range to the code snippet * use range instead of ids * fix articles usage * Grammar fixes * Update docs/core/resilience/http-resilience.md --------- Co-authored-by: David Pine <[email protected]>
1 parent f1ca307 commit 37e086b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

docs/core/resilience/http-resilience.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ To add resilience to an <xref:System.Net.Http.HttpClient>, you chain a call on t
3737
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.
3838

3939
> [!IMPORTANT]
40-
> 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.
40+
> All 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.
4141
4242
## Add standard resilience handler
4343

@@ -77,6 +77,22 @@ Given that you've created an <xref:Microsoft.Extensions.DependencyInjection.IHtt
7777

7878
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.
7979

80+
## Remove standard resilience handlers
81+
82+
There's a method <xref:Microsoft.Extensions.DependencyInjection.ResilienceHttpClientBuilderExtensions.RemoveAllResilienceHandlers*> which removes all previously registered resilience handlers. It's useful when you need to clear existing resilience handlers to add your custom one.
83+
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.
84+
This approach allows you to clear existing configurations and define new ones according to your specific requirements.
85+
86+
:::code language="csharp" source="snippets/http-resilience/Program.RemoveHandlers.cs" range="11-16":::
87+
88+
The preceding code:
89+
90+
- Creates a <xref:Microsoft.Extensions.DependencyInjection.ServiceCollection> instance.
91+
- Adds the standard resilience handler to all <xref:System.Net.Http.HttpClient> instances.
92+
- For the "custom" <xref:System.Net.Http.HttpClient>:
93+
- Removes all predefined resilience handlers that were previously registered. This is useful when you want to start with a clean state to add your own custom strategies.
94+
- 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.
95+
8096
### Standard resilience handler defaults
8197

8298
The default configuration chains five resilience strategies in the following order (from the outermost to the innermost):
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Net;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Http.Resilience;
4+
5+
internal partial class Program
6+
{
7+
private static void RemoveHandlers()
8+
{
9+
#pragma warning disable EXTEXP0001
10+
var services = new ServiceCollection();
11+
// By default, we want all HttpClient instances to include the StandardResilienceHandler.
12+
services.ConfigureHttpClientDefaults(builder => builder.AddStandardResilienceHandler());
13+
// For a named HttpClient "custom" we want to remove the StandardResilienceHandler and add the StandardHedgingHandler instead.
14+
services.AddHttpClient("custom")
15+
.RemoveAllResilienceHandlers()
16+
.AddStandardHedgingHandler();
17+
#pragma warning restore EXTEXP0001
18+
}
19+
}

0 commit comments

Comments
 (0)