I am making a wpf application where i have to consume WebAPI using HttpClient, This is my code.
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:12789/");
//client.DefaultRequestHeaders.Add("appkey", "myapp_key");
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/employee").Result;
if (response.IsSuccessStatusCode)
{
var employees = response.Content.ReadAsAsync<IEnumerable<Employee>>().Result;
grdEmployee.ItemsSource = employees;
}
else
{
MessageBox.Show("Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase);
}
But i am getting this 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?)"
Solution:
After little googling i found the 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.