You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
Activity
lilinus commentedon Oct 15, 2026
I looked into this a bit more and I think the underlying issue is in .NET runtime.
I will open an issue there too.
lilinus commentedon Oct 17, 2026
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.
glennawatson commentedon Oct 18, 2026
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.
fix: disambiguate case-differing parameters, preserve ContentLength h…
github-actions commentedon Nov 16, 2026
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.