" does not contain a definition for "GetAwaiter" and no extension method "GetAwaiter", I am using HttpClient cl...">

"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

+3
c # asp.net-web-api .net-4.0 async-await


source to share


No one has answered this question yet

Check out similar questions:

707
Installed assembly manifest definition does not match assembly reference
638
Does C # have extension properties?
600
How can I run the async Task <T> method synchronously?
57
"System.Net.Http.HttpContent" contains no definition for "ReadAsAsync" and no extension method
50
Task <> does not contain a definition for 'GetAwaiter'
4
does not contain a definition for 'GetAwaiter'
4
Azure Webjob HTTP 409 Conflict Errors
3
How to debug: Have multiple assemblies imported with an equivalent ID?
1
Which assembly has EventProgress in it for Windows Phone 7?
0
Unity xrefs (dll) problem



All Articles
Loading...
X
Show
Funny
Dev
Pics