-
Notifications
You must be signed in to change notification settings - Fork 6k
[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
[Resilient HTTP apps] Add a paragraph describing Http.Resilience RemoveAllResilienceHandlers method #45335
Changes from 1 commit
eddd643
af03af6
c960891
b50e0f1
aa4d410
be592e9
19555cc
893ee54
e419ecd
dc6ebc1
5a98112
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
||
rainsxng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
|
||
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. | ||
rainsxng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- 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. | ||
rainsxng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
Check failure on line 92 in docs/core/resilience/http-resilience.md
|
||
### Standard resilience handler defaults | ||
|
||
The default configuration chains five resilience strategies in the following order (from the outermost to the innermost): | ||
|
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) | ||
rainsxng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// <remove-handlers> | ||
services.ConfigureHttpClientDefaults(builder => builder.AddStandardResilienceHandler()); | ||
Check failure on line 11 in docs/core/resilience/snippets/http-resilience/Program.RemoveHandlers.cs
|
||
rainsxng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// For a named HttpClient "custom" we want to remove the StandardResilienceHandler and add the StandardHedgingHandler instead. | ||
rainsxng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
services.AddHttpClient("custom") | ||
Check failure on line 13 in docs/core/resilience/snippets/http-resilience/Program.RemoveHandlers.cs
|
||
.RemoveAllResilienceHandlers() | ||
.AddStandardHedgingHandler(); | ||
// </remove-handlers> | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.