Error with await operator

时光毁灭记忆、已成空白 提交于 2019-12-20 07:56:58

问题


There is problem with my code. How can I solve this problem? This problem in await operator.

 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);

        }

回答1:


You're trying to use await within a constructor. You can't do that - constructors are always synchronous.

You can only use await within a method or anonymous function with the async modifier; you can't apply that modifier to constructors.

One approach to fixing this would be to create a static async method to create an instance - that would do all the relevant awaiting, and then pass the results to a simple synchronous constructor. Your callers would then need to handle this appropriately, of course.

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);
}



回答2:


You can't use await in the constructor of a class.

An async method returns a Task object which can be executed async. A constructor does not have a return type and thus can't return a Task object, and thus can't be awaited.

A simple fix for this problem is create a Init function:

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);

    }
}

Then when you create your model:

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


来源:https://stackoverflow.com/questions/18205810/error-with-await-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!