Varshini S
asked on
How to fix HttpClient' does not contain a definition for 'DefaultRequestHeaders' error ?
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
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.Cl ient
I got this error:
Install-Package : Unable to find package 'Microsoft.AspNet.WebApi.C lient'.
At line:1 char:1
+ Install-Package Microsoft.AspNet.WebApi.Cl ient
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledExcept ion,NuGet. PowerShell .Commands. InstallPac kageComman d
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"));
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.Cl
I got this error:
Install-Package : Unable to find package 'Microsoft.AspNet.WebApi.C
At line:1 char:1
+ Install-Package Microsoft.AspNet.WebApi.Cl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledExcept
What version of the library are you using?
What version of .NET are you targeting?
ASKER
Framework 4.5.2
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes. it's showing the correct namespace.
ASKER
Thank You Kaufmed and David. The problem got fixed when I changed the target framework to 4.5
Install-Package Microsoft.AspNet.WebApi.Cl
Open in new window