Skip to content

Commit d31b6fd

Browse files
[Resilient HTTP apps] Add a paragraph describing how to disable retries for some HTTP methods (#43630)
1 parent 47d9e00 commit d31b6fd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

docs/core/resilience/http-resilience.md

+10
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ Additionally, these strategies handle the following exceptions:
102102
- `HttpRequestException`
103103
- `TimeoutRejectedException`
104104

105+
#### Disable retries for a given list of HTTP methods
106+
107+
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 the <xref:Microsoft.Extensions.Http.Resilience.HttpRetryStrategyOptionsExtensions.DisableFor(Microsoft.Extensions.Http.Resilience.HttpRetryStrategyOptions,System.Net.Http.HttpMethod[])> method:
108+
109+
:::code language="csharp" source="snippets/http-resilience/Program.RetryOptions.cs" id="disable_for":::
110+
111+
Alternatively, you can use the <xref:Microsoft.Extensions.Http.Resilience.HttpRetryStrategyOptionsExtensions.DisableForUnsafeHttpMethods(Microsoft.Extensions.Http.Resilience.HttpRetryStrategyOptions)> method, which disables retries for `POST`, `PATCH`, `PUT`, `DELETE`, and `CONNECT` requests. According to [RFC](https://www.rfc-editor.org/rfc/rfc7231#section-4.2.1), these methods are considered unsafe; meaning their semantics are not read-only:
112+
113+
:::code language="csharp" source="snippets/http-resilience/Program.RetryOptions.cs" id="disable_for_unsafe_http_methods":::
114+
105115
## Add standard hedging handler
106116

107117
The standard hedging handler wraps the execution of the request with a standard hedging mechanism. Hedging retries slow requests in parallel.

docs/core/resilience/snippets/http-resilience/Program.RetryOptions.cs

+20
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,24 @@ private static void ConfigureRetryOptions(HostApplicationBuilder builder)
1313
builder.Services.Configure<HttpStandardResilienceOptions>(section);
1414
// </options>
1515
}
16+
17+
private static void DisableRetriesFor(IHttpClientBuilder httpClientBuilder)
18+
{
19+
// <disable_for>
20+
httpClientBuilder.AddStandardResilienceHandler(options =>
21+
{
22+
options.Retry.DisableFor(HttpMethod.Post, HttpMethod.Delete);
23+
});
24+
// </disable_for>
25+
}
26+
27+
private static void DisableRetriesForUnsafeHttpMethods(IHttpClientBuilder httpClientBuilder)
28+
{
29+
// <disable_for_unsafe_http_methods>
30+
httpClientBuilder.AddStandardResilienceHandler(options =>
31+
{
32+
options.Retry.DisableForUnsafeHttpMethods();
33+
});
34+
// </disable_for_unsafe_http_methods>
35+
}
1636
}

0 commit comments

Comments
 (0)