Skip to content

[Bug]: IApiResponse.ContentHeaders.ContentLength throws ObjectDisposedException #2357

Closed

Description

@lilinus

Describe the bug 🐞

I'm not able to get the content length of a successful response using IApiResponse.ContentHeaders, it seems like the underlying HttpResponseMessage is already disposed when this happens (ObjectDisposedException is thrown). I only get this exception when the Content-Length header is missing in the response message.

The API I'm calling has the following behaviour:

  • Success: 200 with the body I'm looking for, no Content-Length header.
  • Failure: 200 without body, Content-Length = 0
    I would like to check if Content-Length = 0.

Current workaround is using IApiResponse<T>.HasContent but there is a risk that could be false due to any deserialization error, it also doesn't work for non-generic IApiResponse.

Step to reproduce

using Microsoft.Extensions.DependencyInjection;
using Refit;
using System.Text.Json.Nodes;

await using var sp = new ServiceCollection()
    .AddRefitClient<IClient>()
        .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://testserver.host"))
    .Services.BuildServiceProvider();
var client = sp.GetRequiredService<IClient>();

var resp1 = await client.ResponseHeadersWithContentLength(42);
Console.WriteLine(resp1.ContentHeaders?.ContentLength);
var resp2 = await client.ResponseHeadersWithoutContentLength("test");
Console.WriteLine(resp2.ContentHeaders?.ContentLength);

interface IClient
{
    [Get("/response-headers")]
    Task<IApiResponse<JsonObject>> ResponseHeadersWithContentLength([AliasAs("Content-Length")] long contentLength);
    [Get("/response-headers")]
    Task<IApiResponse<JsonObject>> ResponseHeadersWithoutContentLength([AliasAs("x-Test")] string contentLength);
}

Reproduction repository

No response

Expected behavior

ContentLength should return null since it was missing from response message.

Screenshots 🖼️

No response

IDE

No response

Operating system

No response

Version

No response

Device

No response

Refit Version

16.3.0

Additional information ℹ️

No response

Activity

  1. lilinus commented on Oct 15, 2026

    @lilinus
    Author

    I looked into this a bit more and I think the underlying issue is in .NET runtime.
    I will open an issue there too.

  2. lilinus commented on Oct 17, 2026

    @lilinus
    Author

    Thanks for the very quick fix @glennawatson!

    Im just happy for a fix that works but just want to raise that there might be more problems when disposed objects (request, response headers) are exposed (since it is undefined behaviour). At least to me it wasn't obvious the HttpContentHeaders property value was disposed.

  3. glennawatson commented on Oct 18, 2026

    @glennawatson
    Contributor

    Yeah, I'll see what I can do. I just don't want to do more breaking changes at the moment, so might end up updating the website with the content.

  4. github-actions commented on Nov 16, 2026

    @github-actions

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  5. locked and limited conversation to collaborators on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions