Skip to content
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

AddStandardResilienceHandler doesn't catch HttpRequestException for content length errors. #6071

Open
strigona opened this issue Mar 10, 2025 · 0 comments
Labels
area-resilience bug This issue describes a behavior which is not expected - a bug. untriaged

Comments

@strigona
Copy link

Description

If the API request fails due to a content length mismatch (specifically header Content-Length > than content), we get the following exception System.Net.Http.HttpRequestException: Error while copying content to a stream., however it is not caught by the resilience pipeline.

My understanding is that the extension method(s) inject a DelegatingHandler to wrap the API request and attempt to detect errors and apply the resilience pipeline accordingly. However, the above exception does not get triggered during the DelegatingHandler unless the content is explicitly read within the handler and therefore the pipeline is never applied.

Reproduction Steps

Client:

using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

services.AddHttpClient("my-client")
        .AddStandardResilienceHandler();

// Use the client
HttpClient client = services.BuildServiceProvider()
    .GetRequiredService<IHttpClientFactory>()
    .CreateClient("my-client");

// Make resilient HTTP request
HttpResponseMessage response = await client.GetAsync("http://localhost:5174/badlength");

Server:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/badlength", (HttpContext context) =>
{
    const string content = "This is the response content";
    var incorrectContentLength = content.Length + 10; // Set an incorrect content length

    context.Response.Headers["Content-Length"] = incorrectContentLength.ToString();
    return content;
});

app.Run();

Expected behavior

The HttpRequestException should be caught and retried given it's declared in the policy to retry the exception type.

Actual behavior

The HttpRequestException is not caught and is thrown to the client program immediately.

Regression?

No response

Known Workarounds

No response

Configuration

.net 8.0 + Windows 11

Other information

No response

@strigona strigona added bug This issue describes a behavior which is not expected - a bug. untriaged labels Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-resilience bug This issue describes a behavior which is not expected - a bug. untriaged
Projects
None yet
Development

No branches or pull requests

1 participant