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
{{ message }}
Repository navigation
HttpContentHeaders.ContentLength throws OBjectDisposedException under certain conditions #135859
I'm getting an ObjectDisposedException thrown when trying to access HttpContentHeaders.ContentLength after the HttpContent was disposed, and when the header was neither set in the response nor set when buffering contents.
Note that it is not thrown in some cases, even if HttpContent is disposed (see repro)
I would expect it to return null, or the documentation to indicate ObjectDisposedException can be thrown. No other header in HttpContentHeaders throws if HttpContent is disposed.
var stream = new MemoryStream();
var content = new StreamContent(stream);
//await content.ReadAsStringAsync(); // Does not throw if uncommented
//_ = content.Headers.ContentLength;); // Or if this is uncommented
content.
content.Dispose();
Console.WriteLine(content.Headers.ContentLength);
Expected behavior
ContentLength should return null.
Actual behavior
System.ObjectDisposedException
HResult=0x80131622
Message=Cannot access a disposed object.
Object name: 'System.Net.Http.StreamContent'.
Source=System.Private.CoreLib
StackTrace:
at System.ThrowHelper.ThrowObjectDisposedException(Object instance)
at System.Net.Http.HttpContent.GetComputedOrBufferLength()
at System.Net.Http.Headers.HttpContentHeaders.get_ContentLength()
...
This looks by design IMO, it's a caller issue to use an instance or its state after it has been disposed. Behavior after that is UB, and sometimes throwing ODE is one possible behavior
This looks by design IMO, it's a caller issue to use an instance or its state after it has been disposed. Behavior after that is UB, and sometimes throwing ODE is one possible behavior
Would it make sense to add to the docs that the getter throws ODE? The class doesn't implement IDisposable itself.
Activity
dotnet-policy-service commentedon Oct 15, 2026
Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.
MihaZupan commentedon Oct 15, 2026
This looks by design IMO, it's a caller issue to use an instance or its state after it has been disposed. Behavior after that is UB, and sometimes throwing ODE is one possible behavior
lilinus commentedon Oct 15, 2026
Would it make sense to add to the docs that the getter throws ODE? The class doesn't implement IDisposable itself.