'System.Net.Http.HttpContent' does not contain a definition for 'ReadAsAsync' and no extension method
Solution 1
After a long struggle, I found the solution.
Solution: Add a reference to System.Net.Http.Formatting.dll
. This assembly is also available in the C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies folder.
The method ReadAsAsync
is an extension method declared in the class HttpContentExtensions
, which is in the namespace System.Net.Http
in the library System.Net.Http.Formatting
.
Reflector came to rescue!
Solution 2
Make sure that you have installed the correct NuGet package
in your console application:
<package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" />
and that you are targeting at least .NET 4.0.
This being said, your GetAllFoos
function is defined to return an IEnumerable<Prospect>
whereas in your ReadAsAsync
method you are passing IEnumerable<Foo>
which obviously are not compatible types.
Install-Package Microsoft.AspNet.WebApi.Client
Solution 3
- if you unable to find assembly reference from when (Right click on reference ->add required assembly)
try this
Package manager console
Install-Package System.Net.Http.Formatting.Extension -Version 5.2.3
and then add by using add reference .
Solution 4
Adding a reference to System.Net.Http.Formatting.dll may cause DLL mismatch issues. Right now, System.Net.Http.Formatting.dll appears to reference version 4.5.0.0 of Newtonsoft.Json.DLL, whereas the latest version is 6.0.0.0. That means you'll need to also add a binding redirect to avoid a .NET Assembly exception if you reference the latest Newtonsoft NuGet package or DLL:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
So an alternative solution to adding a reference to System.Net.Http.Formatting.dll is to read the response as a string and then desearalize yourself with JsonConvert.DeserializeObject(responseAsString). The full method would be:
public async Task<T> GetHttpResponseContentAsType(string baseUrl, string subUrl)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(subUrl);
response.EnsureSuccessStatusCode();
var responseAsString = await response.Content.ReadAsStringAsync();
var responseAsConcreteType = JsonConvert.DeserializeObject<T>(responseAsString);
return responseAsConcreteType;
}
}
Solution 5
or if you have VS 2012 you can goto the package manager console and type Install-Package Microsoft.AspNet.WebApi.Client
This would download the latest version of the package
Water Cooler v2
https://sathyaish.net/?c=pros https://www.youtube.com/user/Sathyaish
Updated on November 24, 2021Comments
-
Water Cooler v2 over 2 years
I made a console app to consume a Web API I just made. The console app code does not compile. It gives me the compilation error:
'System.Net.Http.HttpContent' does not contain a definition for 'ReadAsAsync' and no extension method 'ReadAsAsync' accepting a first argument of type 'System.Net.Http.HttpContent' could be found (are you missing a using directive or an assembly reference?)
Here's a test method in which this error occurs.
static IEnumerable<Foo> GetAllFoos() { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("appkey", "myapp_key"); var response = client.GetAsync("http://localhost:57163/api/foo").Result; if (response.IsSuccessStatusCode) return response.Content.ReadAsAsync<IEnumerable<Foo>>().Result.ToList(); } return null; }
I have used this method and consumed it from an MVC client.
-
Water Cooler v2 over 11 yearsThanks. That was a slip, a left over from my effort to remove business related code and replace it with Foos.
-
Water Cooler v2 over 11 yearsI don't understand. I am already targeting the .NET 4.0 framework in my Console app properties. Do I need to set a reference to this library Microsoft.AspNet.WebAPI.Client.dll? I never set any such reference in the ASP.NET MVC project that also consume my Web API and works just fine.
-
Darin Dimitrov over 11 yearsYou need to install the
Microsoft.AspNet.WebApi.Client
NuGet. This will download the latest version from the internet and reference the assembly in your console application. That's exactly what the ASP.NET MVC project template does and is the reason why you don't need to install anything for it to work. But in your console application there's no such thing. -
Water Cooler v2 over 11 yearsThanks. I did what you said. I have 12 projects in my solution, but for some strange reason, after I said 'install-package Microsoft.AspNet.WebApi.Client' in the Library Package Manager console, it printed its usual trace and then said 'Successfully installed Microsoft.AspNet.WebApi.Client in MyMVCProjectNameAndNotMyConsoleProjectName'. The next time I selected my Console project and typed the same thing in the package manager console. It said MyMVCProjectName already references Microsoft.AspNet.WebApi.Client. I am confused.
-
Darin Dimitrov over 11 yearsYeah me too. Start from scratch. New console application, open up the NuGet console, type
Install-Package Microsoft.AspNet.WebApi.Client
and try the code. -
Water Cooler v2 over 11 yearsI just did. Still the same situation. No luck.
-
Darin Dimitrov over 11 yearsOh that's very strange. Because I also did that and it worked perfectly fine in a new console application.
-
Water Cooler v2 over 11 yearsI am using VS 2010 and targeting .NET Framework 4.0 as the profile. Are these the tools you are using?
-
Darin Dimitrov over 11 yearsI am using VS 2012 targeting the full .NET 4.0 profile. Let me try it with VS 2010 as well.
-
Darin Dimitrov over 11 yearsYeah works fine in VS 2010 as well. Even with the .NET 4.0 Client Profile. I suspect that your NuGet system is somehow corrupted and you are not getting the latest version of the
Microsoft.AspNet.WebApi.Client
NuGet from the internet but are using some locally cached version. -
granadaCoder over 10 yearsSelf answers that help others rock the suburbs!
-
bladefist over 10 yearsHow did you get that folder there? I used the web platform installer and it didn't make that folder in Program Files.
-
Joe almost 10 yearsWould you please take a look at this post on a JSON deserialization problem. stackoverflow.com/questions/24131067/… (updated uri)
-
FrankO over 9 yearsAdd Reference -> Assemblies -> Extensions. If it is not listed, go to the Search Assemblies box and type 'formatting'. Hopefully that finds it easier for you.
-
Dan Rayson about 9 yearsThis fixed it for me. - using VS2013
-
user247702 over 7 yearsThe accepted answer from January 2013 already explains this and has more information. Your answer is also not properly formatted.
-
PurpleSmurph over 6 yearsAn update, I found mine here: C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Packages\Microsoft.AspNet.WebApi.Client.4.0.30506.0\lib\net40
-
WernerCD over 6 yearsnuget.org/packages/System.Net.Http.Formatting.Extension - any reason not to use this?
-
M Akin over 6 yearsI had to do this in order for the VSTS Build to work.
-
Naveen over 5 yearsif you already have a Microsoft.AspNet.WebApi.Client package try un installing and installing it, that fixed my issue for v 5.2.3
-
AKS over 4 yearsI was migrating dotnetcore application from 2.2 to 3.1 latest and encounter this error.My app already having reference to System.Net.Http.Formatting.Extension v5.2.3 but my app complain this error , so I installed nuget package Microsoft.AspNet.WebApi.Client as suggested by Rikin and Darin and after that issue is resolved. Thanks
-
pradeepradyumna over 3 yearsAnswered in 2013 and still worked in 2021 for me. This is gem!
-
Merna Mustafa almost 3 yearsThis is just amazing!
-
Dulakshi Soysa over 2 yearssimply install the Microsoft.AspNet.WebApi.Client reference using Nuget problem get solved. first you shd have the System.Net.Http; reference
-
Ashique razak over 2 yearsJust install this package
System.Net.Http.Formatting.Extension
from NuGet -
florian.isopp almost 2 yearsVisual Studio 2022 brand new updates. created the API template and run into this issue. This answer solved my problem, installed via nuget.
-
jeancallisti almost 2 yearsThe "install nuget package Microsoft.AspNet.WebApi.Client" solution (second most upvoted solution) is more modern -- nowadays it makes more sense than referencing a local assembly.