Skip to content

HttpContentHeaders.ContentLength throws OBjectDisposedException under certain conditions #135859

Closed
dotnet/dotnet-api-docs
#13194

Description

@lilinus

Description

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.

More info:
reactiveui/refit#2357

Reproduction Steps

Minimal repro (see reactiveui/refit#2357 for more rea-life-like repro):

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()
...

Regression?

No response

Known Workarounds

Try-catch the exception.

Configuration

No response

Other information

No response

Activity

  1. added
    untriaged
    on Oct 15, 2026
  2. dotnet-policy-service commented on Oct 15, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @karelz, @dotnet/ncl
    See info in area-owners.md if you want to be subscribed.

  3. MihaZupan commented on Oct 15, 2026

    @MihaZupan
    Member

    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

  4. lilinus commented on Oct 15, 2026

    @lilinus
    ContributorAuthor

    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.

  5. added
    documentation
    and removed
    untriaged
    on Oct 28, 2026
  6. added this to the Future milestone on Oct 28, 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

    area-System.Net.Httpdocumentation

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions