"Task <HttpResponseMessage>" does not contain a definition for "GetAwaiter" and no extension method "GetAwaiter",
I am using HttpClient
class libraries in a project (.net4.0)
. To get this working I have added nuget packages for Bcl
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="4.0.30506.0" targetFramework="net40" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net40" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net40" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net40" />
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net40" />
<package id="Newtonsoft.Json" version="4.5.6" targetFramework="net40" />
</packages>
But I still get the error
"Severity Code Description Error suppressing line in project file CS1061" Task "does not contain a definition for" GetAwaiter "and no extension method" GetAwaiter "that takes a first argument of type" Task "can be found (are you missing a using directive or an assembly reference?) "
The code I am using is below
private async Task<bool> Login()
{
try
{
var status = false;
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("username", _userName);
client.DefaultRequestHeaders.Add("password", _password);
using (HttpResponseMessage response = await client.PostAsync(_url, null))
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var contents = await response.Content.ReadAsStringAsync();
AuthenticationResult result = JsonHelper.DeserializeJson<AuthenticationResult>(contents);
if (result != null) _authToken = result.authToken;
}
}
}
return status;
}
catch (Exception ex)
{
return false;
}
}
Strangely, if I change the output type of the project to Windows Application
, the error goes away and it starts working. This error only occurs when the project isClass Library
No one has answered this question yet
Check out similar questions: