Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 等待运算符出错_C#_.net_Async Await - Fatal编程技术网

C# 等待运算符出错

C# 等待运算符出错,c#,.net,async-await,C#,.net,Async Await,我的代码有问题。我怎样才能解决这个问题?这个问题正在等待操作员解决 public MyModel() { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync("https://api.vkontakte.ru/method/video.get?uid=219171498&access_token=d61b93dfde

我的代码有问题。我怎样才能解决这个问题?这个问题正在等待操作员解决

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

        }
您试图在构造函数中使用wait。你不能这么做-构造函数总是同步的

只能在带有异步修饰符的方法或匿名函数中使用wait;不能将该修饰符应用于构造函数

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

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

不能在类的构造函数中使用wait

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

解决此问题的一个简单方法是创建一个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);

    }
}

你能说出问题出在哪里吗?编译时出错?在运行时?至少,给我们错误消息!错误信息呢?行号呢?我无法将方法更改为async,因为您不使用async运算符。尝试将其更改为public async MyModel。最好向我们提供您得到的错误。当前数据库中不存在响应context@JonSkeet我在visual Studio中遇到一个错误,无法找到类型或命名空间名称async,您是否缺少使用指令或程序集引用。我已经声明使用系统;并使用System.Threading.Tasks;事实证明,我使用的是VS2010,根据本页,异步不适用于VS2010。@nueverest:对,这肯定会导致这个问题。。。现在是升级到VS2015的好时机。。。
var model = new MyModel();
await model.Init();