Where is HttpContent.ReadAsAsync?

C#.Netasp.net Web-ApiHttpcontent

C# Problem Overview


I see in tons of examples on the web using the new HttpClient object (as part of the new Web API) that there should be HttpContent.ReadAsAsync<T> method. However, MSDN doesn't mention this method, nor does IntelliSense find it.

Where did it go, and how do I work around it?

C# Solutions


Solution 1 - C#

It looks like it is an extension method (in System.Net.Http.Formatting):

HttpContentExtensions Class

Update:

> PM> install-package Microsoft.AspNet.WebApi.Client

According to the System.Net.Http.Formatting NuGet package page, the System.Net.Http.Formatting package is now legacy and can instead be found in the Microsoft.AspNet.WebApi.Client package available on NuGet here.

Solution 2 - C#

I have the same problem, so I simply get JSON string and deserialize to my class:

HttpResponseMessage response = await client.GetAsync("Products");
//get data as Json string 
string data = await response.Content.ReadAsStringAsync();
//use JavaScriptSerializer from System.Web.Script.Serialization
JavaScriptSerializer JSserializer = new JavaScriptSerializer();
//deserialize to your class
products = JSserializer.Deserialize<List<Product>>(data);

Solution 3 - C#

If you are already using Newtonsoft.Json and don't want to install Microsoft.AspNet.WebApi.Client:

 var myInstance = JsonConvert.DeserializeObject<MyClass>(
   await response.Content.ReadAsStringAsync());

Solution 4 - C#

2021 Update: Looks like the method is removed in .NET5. Alternatively, you can use ReadFromJsonAsync<>() from System.Net.Http.Json.HttpContentJsonExtensions. It solves the purpose.

Solution 5 - C#

You can write extention method:

public static async Task<Tout> ReadAsAsync<Tout>(this System.Net.Http.HttpContent content) {
	return Newtonsoft.Json.JsonConvert.DeserializeObject<Tout>(await content.ReadAsStringAsync());
}

Solution 6 - C#

Just right click in your project go Manage NuGet Packages search for Microsoft.AspNet.WebApi.Client install it and you will have access to the extension method.

Solution 7 - C#

Having hit this one a few times and followed a bunch of suggestions, if you don't find it available after installing the NuGet Microsoft.AspNet.WebApi.Client manually add a reference from the packages folder in the solution to:

\Microsoft.AspNet.WebApi.Client.5.2.6\lib\net45\System.Net.Http.Formatting.dll

And don't get into the trap of adding older references to the System.Net.Http.Formatting.dll NuGet

Solution 8 - C#

Although I had the same problem, the answers in this thread didn't fully help me to fix the problem. For this reason, I decided to write the result of my research in this post. To fix this issue, follow the steps below:

  1. Add the Microsoft.AspNet.WebApi.Client package to the project using NuGet. While inside the ASP.NET solution, open the Package Manager Console by going to Tools > NuGet Package Manager > Package Manager Console in Visual Studio IDE and add the Microsoft.AspNet.WebApi.Client package to the solution.
Install-Package Microsoft.AspNet.WebApi.Client -Version 5.2.7
  1. After its installation, check that the extensions DLL exists on your system. System.Net.Http.Formatting.dll file should be present in the directory shown below as a result of the first step.
{root-solution-directory}\packages\Microsoft.AspNet.WebApi.Client.{package-version}\lib\net45\
  1. Manually add the reference to the relevant project. Right click on the "References" section in the ASP.NET project within the solution click on the "Add Reference..." section. Since the file System.Net.Http.Formatting.dll is an extension, it will not be listed when searched directly like other items in the Microsoft.AspNet.WebApi.Client package. Therefore, to add the DLL file manually, click the "Browse..." button at the bottom of the "Reference Manager" window. Select the System.Net.Http.Formatting.dll file in the directory shown in the second step and check the checkbox to include the DLL file in the project.

  2. Include the System.Net.Http namespace in the project to use the features provided by this DLL in the project; using System.Net.Http.Formatting; declaration is available within the HttpContentExtensions static class.

using System.Net.Http;

OPTIONAL: You can achieve a similar solution by installing one of the System.Net.Http.Formatting.Extension or WebApiDoodle.Net.Http.Formatting packages and following the steps above.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid PfefferView Question on Stackoverflow
Solution 1 - C#J...View Answer on Stackoverflow
Solution 2 - C#rostaView Answer on Stackoverflow
Solution 3 - C#Martin BrandlView Answer on Stackoverflow
Solution 4 - C#MangeshView Answer on Stackoverflow
Solution 5 - C#Vasya MilovidovView Answer on Stackoverflow
Solution 6 - C#Ivan Carmenates GarcíaView Answer on Stackoverflow
Solution 7 - C#Tom JohnView Answer on Stackoverflow
Solution 8 - C#SercanView Answer on Stackoverflow