135

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.

8 Answers 8

168

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!

6
  • 1
    How did you get that folder there? I used the web platform installer and it didn't make that folder in Program Files.
    – bladefist
    Commented Jan 13, 2014 at 19:02
  • 7
    Add Reference -> Assemblies -> Extensions. If it is not listed, go to the Search Assemblies box and type 'formatting'. Hopefully that finds it easier for you.
    – FrankO
    Commented Oct 13, 2014 at 18:19
  • 2
    An 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 Commented Sep 25, 2017 at 14:51
  • 12
    nuget.org/packages/System.Net.Http.Formatting.Extension - any reason not to use this?
    – WernerCD
    Commented Oct 2, 2017 at 22:48
  • 3
    Just install this package System.Net.Http.Formatting.Extension from NuGet Commented Jan 24, 2022 at 5:35
135

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

Select project in project manager console

14
  • Thanks. That was a slip, a left over from my effort to remove business related code and replace it with Foos. Commented Jan 25, 2013 at 11:41
  • I 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. Commented Jan 25, 2013 at 11:46
  • 5
    You 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. Commented Jan 25, 2013 at 11:47
  • 1
    Thanks. 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. Commented Jan 25, 2013 at 12:16
  • Yeah me too. Start from scratch. New console application, open up the NuGet console, type Install-Package Microsoft.AspNet.WebApi.Client and try the code. Commented Jan 25, 2013 at 12:20
39
  • 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 .

1
  • 3
    I 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
    – AKS
    Commented Jan 9, 2020 at 16:09
15

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;
      }
}
9

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

2
  • This fixed it for me. - using VS2013
    – Dan Rayson
    Commented Apr 2, 2015 at 13:12
  • if you already have a Microsoft.AspNet.WebApi.Client package try un installing and installing it, that fixed my issue for v 5.2.3
    – Naveen
    Commented Dec 5, 2018 at 16:25
5

Install below package from nuget enter image description here

4

Had the same issue, but proposed answers didn't help, installing System.Net.Http.Json package removed the error.

-2

USE This Assembly Referance in your Project

Add a reference to System.Net.Http.Formatting.dll
1
  • 9
    The accepted answer from January 2013 already explains this and has more information. Your answer is also not properly formatted.
    – user247702
    Commented Nov 8, 2016 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.