【问题标题】:Error with await operator等待运算符出错
【发布时间】:2013-08-14 21:00:39
【问题描述】:

我的代码有问题。我怎么解决这个问题? await 运算符中的这个问题。

 public MyModel() 
    {
        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync("https://api.vkontakte.ru/method/video.get?uid=219171498&access_token=d61b93dfded2a37dfcfa63779efdb149653292636cac442e53dae9ba6a049a75637143e318cc79e826149");
        string googleSearchText = await response.Content.ReadAsStringAsync();
        JObject googleSearch = JObject.Parse(googleSearchText);
        IList<JToken> results = googleSearch["response"].Children().Skip(1).ToList();
        IList<MainPage1> searchResults = new List<MainPage1>();
        foreach (JToken result in results)
        {
            MainPage1 searchResult = JsonConvert.DeserializeObject<MainPage1>(result.ToString());
            searchResults.Add(searchResult);

        }

【问题讨论】:

  • 你能用语言描述问题是什么吗?编译时出错?在运行时?
  • 至少,给我们错误信息!
  • 错误信息呢?行号呢?
  • 我无法将方法更改为异步
  • 您不使用async 运算符。试着把它改成public async MyModel()。最好把你得到的错误告诉我们。

标签: c# .net async-await


【解决方案1】:

您正在尝试在构造函数中使用await。你不能这样做 - 构造函数总是同步的。

您只能在带有async 修饰符的方法或匿名函数中使用await;您不能将该修饰符应用于构造函数。

解决此问题的一种方法是创建一个 static 异步方法来创建一个实例 - 这将完成所有相关的等待,然后将结果传递给一个简单的同步构造函数。当然,您的调用者需要适当地处理这个问题。

public static async Task<MyModel> CreateInstance()
{
    string googleSearchText;
    using (HttpClient client = new HttpClient())
    {
        using (HttpResponseMessage response = await client.GetAsync(...))
        {
            googleSearchText = await response.Content.ReadAsStringAsync();
        }
    }
    // Synchronous constructor to do the rest...
    return new MyModel(googleSearchText);
}

【讨论】:

  • “响应”在当前上下文中不存在
  • @JonSkeet 我在视觉工作室 The type or namespace name async could not be found (are you missing using directive or assembly reference 中遇到错误。我已经声明了using System;using System.Threading.Tasks;
  • 事实证明我使用的是 VS2010,根据此页面 nuget.org/packages/Microsoft.Bcl.Async async 不适用于 VS2010。
  • @nueverest:对,那肯定会导致这个问题……现在是升级到 VS2015 的好时机……
【解决方案2】:

你不能在类的构造函数中使用 await。

异步方法返回一个可以异步执行的Task 对象。构造函数没有返回类型,因此无法返回 Task 对象,因此无法等待。

解决这个问题的一个简单方法是创建一个 Init 函数:

public MyModel() 
{

}

public async Task Init()
{
    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync("https://api.vkontakte.ru/method/video.get?uid=219171498&access_token=d61b93dfded2a37dfcfa63779efdb149653292636cac442e53dae9ba6a049a75637143e318cc79e826149");
    string googleSearchText = await response.Content.ReadAsStringAsync();
    JObject googleSearch = JObject.Parse(googleSearchText);
    IList<JToken> results = googleSearch["response"].Children().Skip(1).ToList();
    IList<MainPage1> searchResults = new List<MainPage1>();
    foreach (JToken result in results)
    {
        MainPage1 searchResult = JsonConvert.DeserializeObject<MainPage1>(result.ToString());
        searchResults.Add(searchResult);

    }
}

然后当你创建你的模型时:

var model = new MyModel();
await model.Init();

【讨论】:

    猜你喜欢
    • 2019-10-19
    • 2016-02-09
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2019-01-03
    • 2020-09-19
    相关资源
    最近更新 更多