From 28e807798f224e9d3200a833a466e68e3066fb5d Mon Sep 17 00:00:00 2001 From: Iliar Turdushev Date: Mon, 18 Nov 2024 19:05:24 +0100 Subject: [PATCH 1/4] Fixes #5248 Adds description how to disable retries for some HTTP methods --- docs/core/resilience/http-resilience.md | 10 ++++++++++ .../http-resilience/Program.RetryOptions.cs | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/docs/core/resilience/http-resilience.md b/docs/core/resilience/http-resilience.md index 8ec42c1d20172..a3edb40a8ca18 100644 --- a/docs/core/resilience/http-resilience.md +++ b/docs/core/resilience/http-resilience.md @@ -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 for that: + +:::code language="csharp" source="snippets/http-resilience/Program.RetryOptions.cs" id="disable_for"::: + +Alternatively, you can use a method 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. diff --git a/docs/core/resilience/snippets/http-resilience/Program.RetryOptions.cs b/docs/core/resilience/snippets/http-resilience/Program.RetryOptions.cs index f49b9d68c829e..080682b91b862 100644 --- a/docs/core/resilience/snippets/http-resilience/Program.RetryOptions.cs +++ b/docs/core/resilience/snippets/http-resilience/Program.RetryOptions.cs @@ -13,4 +13,24 @@ private static void ConfigureRetryOptions(HostApplicationBuilder builder) builder.Services.Configure(section); // } + + private static void DisableRetriesFor(IHttpClientBuilder httpClientBuilder) + { + // + httpClientBuilder.AddStandardResilienceHandler(options => + { + options.Retry.DisableFor(HttpMethod.Post, HttpMethod.Delete); + }); + // + } + + private static void DisableRetriesForUnsafeHttpMethods(IHttpClientBuilder httpClientBuilder) + { + // + httpClientBuilder.AddStandardResilienceHandler(options => + { + options.Retry.DisableForUnsafeHttpMethods(); + }); + // + } } From fa4f0a0053a780d35077897c3b089a71beae8f92 Mon Sep 17 00:00:00 2001 From: David Pine Date: Wed, 20 Nov 2024 08:26:05 -0600 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Darius Letterman --- docs/core/resilience/http-resilience.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/core/resilience/http-resilience.md b/docs/core/resilience/http-resilience.md index a3edb40a8ca18..2e7f06fafa615 100644 --- a/docs/core/resilience/http-resilience.md +++ b/docs/core/resilience/http-resilience.md @@ -102,13 +102,13 @@ Additionally, these strategies handle the following exceptions: - `HttpRequestException` - `TimeoutRejectedException` -#### Disabling retries for a given list of HTTP methods +#### Disable 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 for that: +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 method: :::code language="csharp" source="snippets/http-resilience/Program.RetryOptions.cs" id="disable_for"::: -Alternatively, you can use a method 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: +Alternatively, you can use the 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: :::code language="csharp" source="snippets/http-resilience/Program.RetryOptions.cs" id="disable_for_unsafe_http_methods"::: From 9147b176ca3ca85ea0d3a3bcac45e90389f0c2c0 Mon Sep 17 00:00:00 2001 From: Iliar Turdushev Date: Wed, 22 Jan 2025 14:50:53 +0100 Subject: [PATCH 3/4] Updates the M.E.Http.Resilience package version --- .../resilience/snippets/http-resilience/http-resilience.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/resilience/snippets/http-resilience/http-resilience.csproj b/docs/core/resilience/snippets/http-resilience/http-resilience.csproj index ec467dee36e37..702cfba8ecd4a 100644 --- a/docs/core/resilience/snippets/http-resilience/http-resilience.csproj +++ b/docs/core/resilience/snippets/http-resilience/http-resilience.csproj @@ -20,7 +20,7 @@ - + From 9f7f1e5936327ddd206ec77ab4699c85e70d3793 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Thu, 23 Jan 2025 08:55:21 -0800 Subject: [PATCH 4/4] Update xrefs --- docs/core/resilience/http-resilience.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/core/resilience/http-resilience.md b/docs/core/resilience/http-resilience.md index 2e7f06fafa615..79e64b5097d25 100644 --- a/docs/core/resilience/http-resilience.md +++ b/docs/core/resilience/http-resilience.md @@ -104,11 +104,11 @@ Additionally, these strategies handle the following exceptions: #### Disable 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 the method: +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 method: :::code language="csharp" source="snippets/http-resilience/Program.RetryOptions.cs" id="disable_for"::: -Alternatively, you can use the 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: +Alternatively, you can use the 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: :::code language="csharp" source="snippets/http-resilience/Program.RetryOptions.cs" id="disable_for_unsafe_http_methods":::