Keywords: C# | .NET | HttpClient | ReadAsAsync | System.Net.Http.Formatting
Abstract: When developing a console application to consume a Web API in C#, you might encounter a compilation error stating that 'System.Net.Http.HttpContent' does not contain a definition for 'ReadAsAsync'. This article explains the cause of this error and provides solutions, primarily by adding a reference to System.Net.Http.Formatting.dll or installing the Microsoft.AspNet.WebApi.Client NuGet package.
Problem Description
When working with the HttpClient class in C# to consume a Web API, you may write code similar to the following:
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;
}
However, this code fails to compile with the 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.
Error Cause
The ReadAsAsync method is an extension method defined in the System.Net.Http.Formatting assembly. By default, when you create a new project, this assembly might not be referenced. The extension method is part of the HttpContentExtensions class in the System.Net.Http namespace within the System.Net.Http.Formatting.dll.
Primary Solution
To resolve this issue, add a reference to System.Net.Http.Formatting.dll. You can find this assembly in the installation directory, such as C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies. Alternatively, for modern development, it is recommended to use NuGet packages.
Alternative Solutions
Another common solution is to install the Microsoft.AspNet.WebApi.Client NuGet package. This package includes the necessary assemblies and is compatible with .NET 4.0 and above. You can install it via the Package Manager Console using the command: Install-Package Microsoft.AspNet.WebApi.Client.
Additionally, ensure that your project targets at least .NET Framework 4.0 and check for type compatibility in your code, as mentioned in other answers.
Code Example with Fix
Here is an updated version of the code with the necessary reference added:
// Ensure to add reference to System.Net.Http.Formatting or install the NuGet package
using System.Net.Http;
using System.Net.Http.Formatting; // This namespace contains HttpContentExtensions
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)
{
// Now ReadAsAsync is available
var result = response.Content.ReadAsAsync<IEnumerable<Foo>>().Result;
return result.ToList();
}
}
return null;
}
Conclusion
The compilation error for ReadAsAsync is typically due to missing references. By adding System.Net.Http.Formatting.dll or installing the appropriate NuGet package, you can easily resolve the issue and successfully use the extension method in your C# applications.