与计谋操作失误计谋、操作

由网友(待我强大我将扫遍所有零食)分享简介:有问题,我的code。我该如何解决这个问题呢?这个问题在等待着运营商。公开为MyModel(){HttpClient的客户端=新的HttpClient();HTT presponseMessage响应=等待client.GetAsync("https://api.vkontakte.ru/method/video.ge...

有问题,我的code。我该如何解决这个问题呢?这个问题在等待着运营商。

 公开为MyModel()
    {
        HttpClient的客户端=新的HttpClient();
        HTT presponseMessage响应=等待client.GetAsync("https://api.vkontakte.ru/method/video.get?uid=219171498&access_token=d61b93dfded2a37dfcfa63779efdb149653292636cac442e53dae9ba6a049a75637143e318cc79e826149");
        字符串googleSearchText =等待response.Content.ReadAsStringAsync();
        JObject的GoogleSearch = JObject.Parse(googleSearchText);
        IList的< JToken>结果=的GoogleSearch [回应]儿童()跳过(1).ToList()。
        IList的< MainPage1>的SearchResult =新的名单,其中,MainPage1>();
        的foreach(JToken导致的结果)
        {
            MainPage1信息搜索结果= JsonConvert.DeserializeObject&其中; MainPage1>(result.ToString());
            sea​​rchResults.Add(信息搜索结果);

        }
 

解决方案

您正在尝试使用等待一个构造函数中。你可以这样做 - 构造函数的总是的同步

猪屁登 奶奶占用公共设施晒衣物,屁登劝阻无果,只好使用计谋

您只能使用等待的方法或匿名函数与异步修改器中;您可以在修改不适用的构造。

一种方法来修复,这将是创建一个静态的异步方法来创建一个实例 - 这将做所有的相关的等待,然后将结果传递给一个简单的同步构造。您的呼叫然后将需要妥善处理,这当然。

 公共静态异步任务<为MyModel>的CreateInstance()
{
    串googleSearchText;
    使用(HttpClient的客户端=新的HttpClient())
    {
        使用(Htt的presponseMessage响应=等待client.GetAsync(...))
        {
            googleSearchText =等待response.Content.ReadAsStringAsync();
        }
    }
    //同步构造函数来完成剩下的...
    返回新为MyModel(googleSearchText);
}
 

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

        }

解决方案

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

阅读全文

相关推荐

最新文章