Coffeehouse Thread

2 posts

'System.Net.Http.HttpContent' does not contain a definition for 'ReadAsAsync' and no extension method

Back to Forum: Coffeehouse
  • Sathyaish Chakravarthy

    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.

  • Sathyaish Chakravarthy

    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 extention 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!

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.