I am using HttpClient in a class library project (.net4.0). In order 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 message
"Severity Code Description Project File Line Suppression State Error CS1061 'Task' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?)"
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;
}
}
Strange thing is if I change the project output type to Windows Application error goes away and this start working. This error only comes when the project is a Class Library