dotnet / dotnet/dotnet-api-docs

HttpClient.PostAsync disposes the HttpContent object in .NET Framework

Open
#3,792 2 comments 0 reactions 0 assignees View on GitHub
area-System.Net Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

On the following page:
[https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.postasync?view=netframework-4.8#definition](https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.postasync?view=netframework-4.8#definition)

Under the "**Remarks**" section, please include the following information:

The **PostAsync** method will automatically dispose the HttpContent object passed to it. If your use case requires retry requests, you will need to create a new HttpContent object before calling PostAsync.

The above tidbit of information is extremely useful when implementing code that implements "retry" attempts. Failure to adhere to the above recommendation will result in the following exception being raised:

Cannot access a disposed object. Object name: 'System.Net.Http.StringContent' exception.

Looking at the code for PostAsync, it appears the cancellation token is also disposed of -- might be useful to mention that in the remarks as well. I have not looked at other methods to see if this behavior is the same.

Thanks.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.