356

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?

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202

8 Answers8

533

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.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
J...
  • 30,968
  • 6
  • 66
  • 143
  • 1
    Has this been deprecated in the latest client Nugets? Can't find it now (used to be able to). – georgiosd Oct 03 '13 at 12:07
  • 1
    It could well be. .NET4.5 added a lot of new stuff as regards async/tasks (await, etc), so these extensions may no longer be necessary. I'd maybe have a look here, for example : http://msdn.microsoft.com/en-us/library/system.net.http.httpcontent.aspx – J... Oct 03 '13 at 12:27
  • I can get the same behavior with `ReadAsAsync` and specifying a JSON formatter but it was a handy shortcut. – georgiosd Oct 06 '13 at 10:30
  • 4
    @georgiosd I've just updated the answer. In the mean time `System.Net.Http.Formatting` looks like it's moved to http://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client/ package. – Joseph Woodward Oct 25 '13 at 13:59
  • I tried for several hours and never got this to work. Using `JavaScriptSerializer` instead. – Jess Jun 24 '14 at 20:24
  • I'm creating an ASP.NET MVC 5 application can I use that package? – vcRobe Oct 21 '15 at 20:06
  • @vcRobe Suggest you ask that as a new question. – J... Oct 21 '15 at 20:17
  • 1
    So is it true that the method in the original question, `HttpContent.ReadAsAsync`, is not in Microsoft.AspNet.WebApi.Client? What is the suggested replacement? – Patrick Szalapski Aug 24 '16 at 13:58
  • I see the suggested replacement in the other answer. – Patrick Szalapski Aug 24 '16 at 14:04
  • 2
    @JedatKinports Quite sure it doesn't. The WebAPI.Client package require .NET 4.5. I think the old HttpContentExtensions were for .NET 4.0 – J... Oct 09 '17 at 13:59
75

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);
rosta
  • 897
  • 7
  • 2
  • 8
    Aside from the fact that this does not answer the question, why did this get voted down? Isn't it a reasonable alternative to `ReadAsAsync`? – Jess Jun 24 '14 at 20:42
  • 9
    probably because you didn't answer the question of where did the extension method go. Writing your own is a poor workaround – BritishDeveloper Apr 29 '16 at 11:25
  • 16
    It is not necessarily a poor workaround, it is localised and hence not likely to get broken in future nuget updates etc. I'm all for it. – rollsch Feb 12 '17 at 04:34
  • 3
    This assumes the reponse has a JSON content-type. – CodeCaster May 24 '17 at 09:49
  • I think ReadAsStringAsync is a bad idea, using ReadAsStreamAsync it is a better choice in the solution that is not an answer to asked question at all :) – maciejW Jul 20 '19 at 16:56
  • 1
    Folks -- This answers 1/2 the question pretty clearly -- "and how do I work around it". +1+1 (the other plus one represents the three plus ones I gave to people who found this answer useful!) – Gerard ONeill Sep 30 '19 at 19:19
  • 1
    It's important to note this isn't feature par. You cause unnecessary allocations to read as a `string` just to turn into `T`, and you ignore the content type header. The source of `ReadAsAsync` [here](https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Net.Http.Formatting/HttpContentExtensions.cs#L266) shows it toggles the formatter used and passes the formatter the `Stream` to deserialize, like the [JSON one](https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Net.Http.Formatting/Formatting/BaseJsonMediaTypeFormatter.cs#L245). – benmccallum Feb 04 '20 at 18:27
  • That being said, `System.Text.Json` in .NET Core 3+ is the new kid in town and will probably underpin `ReadAsAsync` in due course. In the meantime, you can just do `JsonSerializer.DeserializeAsync(await response.Content.ReadAsStreamAsync());` if you don't care about the content type. – benmccallum Feb 04 '20 at 18:37
50

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());
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • This is what I ended up doing. Trying to use the new `System.Text.Json` instead proved too much of a hassle due to "Allow non-string JSON values for string properties" not being supported. – Daniel Congrove Dec 30 '20 at 14:55
30

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.

Mangesh
  • 5,491
  • 5
  • 48
  • 71
19

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());
}
Vas Mil
  • 605
  • 1
  • 8
  • 21
  • 2
    It's what I ended up doing - I'm not going to drag an entire DLL for a 2-line method. – Liz Jun 20 '19 at 09:13
  • 1
    @VasyaMilovidov Thank you for sharing your code. Before your suggestion, I spent hours without any success. Your suggestion worked like a charm for my need. As `@Liz` mentioned no need to install `Microsoft.AspNet.WebApi.Client` with unnecessary DLLS (at least in my scenario). – nam Oct 12 '20 at 04:26
  • 1
    Nice to see the old questions, with newer answers still being relevant 10 years later! – callisto Dec 06 '22 at 09:06
7

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.

1

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

Tom John
  • 783
  • 5
  • 14
0

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.

Sercan
  • 4,739
  • 3
  • 17
  • 36