Skip to content

[Resilient HTTP apps] Add a paragraph describing how to disable retries for some HTTP methods #43630

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 6 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions docs/core/resilience/http-resilience.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ Additionally, these strategies handle the following exceptions:
- `HttpRequestException`
- `TimeoutRejectedException`

#### Disabling retries for a given list of HTTP methods

By default, the standard resilience handler is configured to make retries for all HTTP methods. For some applications such behavior could be undesirable or even harmful. For example, if a POST request inserts a new record to a database, then making retries for such a request could lead to data duplication. If you need to disable retries for a given list of HTTP methods you can use a method <xref:Microsoft.Extensions.Http.Resilience.HttpRetryStrategyOptionsExtensions.DisableFor> for that:

:::code language="csharp" source="snippets/http-resilience/Program.RetryOptions.cs" id="disable_for":::

Alternatively, you can use a method <xref:Microsoft.Extensions.Http.Resilience.HttpRetryStrategyOptionsExtensions.DisableForUnsafeHttpMethods> which disables retries for `POST`, `PATCH`, `PUT`, `DELETE`, and `CONNECT` HTTP methods. According to [RFC](https://www.rfc-editor.org/rfc/rfc7231#section-4.2.1) these methods are considered unsafe meaning their semantics is non readonly:

:::code language="csharp" source="snippets/http-resilience/Program.RetryOptions.cs" id="disable_for_unsafe_http_methods":::

## Add standard hedging handler

The standard hedging handler wraps the execution of the request with a standard hedging mechanism. Hedging retries slow requests in parallel.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,24 @@
builder.Services.Configure<HttpStandardResilienceOptions>(section);
// </options>
}

private static void DisableRetriesFor(IHttpClientBuilder httpClientBuilder)
{
// <disable_for>
httpClientBuilder.AddStandardResilienceHandler(options =>
{
options.Retry.DisableFor(HttpMethod.Post, HttpMethod.Delete);

Check failure on line 22 in docs/core/resilience/snippets/http-resilience/Program.RetryOptions.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\core\resilience\snippets\http-resilience\Program.RetryOptions.cs(22,27): error CS1061: 'HttpRetryStrategyOptions' does not contain a definition for 'DisableFor' and no accessible extension method 'DisableFor' accepting a first argument of type 'HttpRetryStrategyOptions' could be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\core\resilience\snippets\http-resilience\http-resilience.csproj]
});
// </disable_for>
}

private static void DisableRetriesForUnsafeHttpMethods(IHttpClientBuilder httpClientBuilder)
{
// <disable_for_unsafe_http_methods>
httpClientBuilder.AddStandardResilienceHandler(options =>
{
options.Retry.DisableForUnsafeHttpMethods();

Check failure on line 32 in docs/core/resilience/snippets/http-resilience/Program.RetryOptions.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\core\resilience\snippets\http-resilience\Program.RetryOptions.cs(32,27): error CS1061: 'HttpRetryStrategyOptions' does not contain a definition for 'DisableForUnsafeHttpMethods' and no accessible extension method 'DisableForUnsafeHttpMethods' accepting a first argument of type 'HttpRetryStrategyOptions' could be found (are you missing a using directive or an assembly reference?) [D:\a\docs\docs\docs\core\resilience\snippets\http-resilience\http-resilience.csproj]
});
// </disable_for_unsafe_http_methods>
}
}
Loading