How to fix HttpClient' does not contain a definition for 'DefaultRequestHeaders' error ?

Varshini S
Varshini S used Ask the Experts™
on
I'm getting below error while compiling the below code

HttpClient' does not contain a definition for 'DefaultRequestHeaders'
HttpClient' does not contain a definition for 'GetAsync'

Project details:
Microsoft Visual C# 2012
.Net framework 4.5.2
Windows service

using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

var client = new HttpClient();
 client.BaseAddress = new Uri(ConfigurationManager.AppSettings["uri-path-port"].ToString());

 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
           

Open in new window


HttpClient' does not contain a definition for 'DefaultRequestHeaders'
HttpClient' does not contain a definition for 'GetAsync'

To fix this issue I  have tried the below  form package manager console:

Install-Package Microsoft.AspNet.WebApi.Client

I got this error:

Install-Package : Unable to find package 'Microsoft.AspNet.WebApi.Client'.
At line:1 char:1
+ Install-Package Microsoft.AspNet.WebApi.Client
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
This Builds Correctly  
Install-Package Microsoft.AspNet.WebApi.Client isn't a nuget package use the website properties add reference instead.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http.Headers;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri(ConfigurationManager.AppSettings["uri-path-port"].ToString());

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));




    }
}

Open in new window

Author

Commented:
Hi David,

The reference has been already added.
reference.PNG
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
What version of the library are you using?
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Author

Commented:
version.png
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
What version of .NET are you targeting?

Author

Commented:
Framework 4.5.2
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
I'm at a loss then. Those versions should be fine, and the only time I've seen stuff like this is when an older version of either is being targeted/referenced.
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
If you hover over HttpClient, is it showing the correct namespace?

Author

Commented:
Yes. it's showing the correct namespace.

Author

Commented:
Thank You Kaufmed and David. The problem got fixed when I changed the target framework to 4.5 

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial