类型或命名空间名称“异步”找不到找不到、名称、类型、空间

2023-09-04 07:42:35 作者:凉陌如花

我尝试使用下面的方法在WPF应用程序 .NET Framework 4的客户端配置文件但我收到此错误:

I am trying to use the following method in a WPF application .NET Framework 4 Client Profile but I receive this error:

的类型或命名空间名称异步无法找到的

我使用

using System.Threading.Tasks;

任何想法可能是错误的?在此先感谢

Any idea what could be wrong? Thanks in advance

private async Task SumPageSizesAsync()
{
    HttpClient client = new HttpClient();
    Task<byte[]> getContentsTask = client.GetByteArrayAsync(url);
    byte[] urlContents = await getContentsTask;   
}

我使用VS 2010

I am using VS 2010

推荐答案

那么,有两件事情:

您需使用C#编译器5,例如VS2012。如果你使用VS2010,您不能使用异步。鉴于此错误信息,我怀疑你使用了错误的编译器版本。 您需要使用 Microsoft.Bcl.Async 的NuGet包带来的.NET 4的相应的库支持。