&#39;任务<httpresponsemessage>&#39;不包含&#39; GetAwaiter&#39;的定义没有扩展方法&#39; GetAwaiter&#39;

时间:2017-05-30 08:50:12

标签: c# asp.net-web-api .net-4.0 async-await

我在类库项目HttpClient中使用(.net4.0)。为了实现这个目的,我为Bcl添加了nuget包

<?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>

但我仍然收到错误消息

  

&#34;严重级代码说明项目文件行抑制状态错误CS1061&#39;任务&#39;不包含定义   为了GetAwaiter&#39;没有扩展方法&#39; GetAwaiter&#39;接受一个   类型&#39;任务&#39;的第一个参数可以找到(是   你错过了using指令或程序集引用?)&#34;

我正在使用的代码在

之下
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;
    }

}

奇怪的是,如果我将项目输出类型更改为Windows Application,则错误消失并开始工作。仅当项目为Class Library

时才会出现此错误

0 个答案:

没有答案