制作异步休息请求

时间:2014-07-27 18:53:32

标签: c# .net rest windows-phone-8 restsharp

我正在使用RestSharp在Windows Phone上发布一些Rest请求。

但我正在努力使用“Aync / await”和我使用的函数:

例如使用此功能:

   private  void Function() 
        {
            var client = new RestSharp.RestClient("https://exampleapi.com");
            client.Authenticator = [....]
            var request =  new RestSharp.RestRequest("/example.json", Method.GET);
            try
            {
                client.ExecuteAsync(request, reponse =>
                {
                    if (reponse.StatusCode == HttpStatusCode.OK)
                    {


              // Operations with the json...
                    }
                    else
                    {
                        MessageBox.Show("Error");
                    }
                });
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }

我试图像这样添加async这个词:

private async void restsharptest() 
            {
                var client = new RestSharp.RestClient("https://exampleapi.com");
                client.Authenticator = [....]
                var request =  new RestSharp.RestRequest("/example.json", Method.GET);
                try
                {
                    client.ExecuteAsync(request, reponse =>
                    {
                        if (reponse.StatusCode == HttpStatusCode.OK)
                        {


  var timeline =  JsonConvert.DeserializeObject<List<dynamic>>(reponse.Content);
                        }
                        else
                        {
                            MessageBox.Show("Error");
                        }
                    });
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                }
            }

Buth然后当我尝试添加等待时:

var timeline = await  JsonConvert.DeserializeObject<List<dynamic>>(reponse.Content);

我收到以下错误:

  

无法访问'System.Collections.Generic.List

和:

  

运算符'await'只能在lambda表达式async中使用。标记   这与lambda表达式修饰符'async'。

如何使用async/await我的“Function1”?

编辑:

 client.ExecuteAsync(request, response  =>
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    List_ = new List<Myobject>();
                    List_ = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<List<Myobject>>(response.Content));
                    tcs.SetResult(RefreshList_);
                }
                else
                {
                    MessageBox.Show("Error");
                }
            });

我再次遇到此错误:

  

运算符'await'只能在lambda表达式async中使用。标记   这与lambda表达式修饰符'async'。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

要使用异步,您应该执行以下操作:

private async void Function() 
{
     var client = new RestSharp.RestClient("https://exampleapi.com");
     client.Authenticator = [....]
     var request =  new RestSharp.RestRequest("/example.json", Method.GET);
     try
     {
         var response = await client.ExecuteTaskAsync(request);

         if (reponse.StatusCode == HttpStatusCode.OK)
         {
             // Operations with the json...
         }
         else
         {
             MessageBox.Show("Error");
         }
     }
     catch (Exception error)
     {
         MessageBox.Show(error.Message);
     }
  }

请注意ExecuteAsyncExecuteTaskAsync之间存在差异。

答案 1 :(得分:1)

您只能await(等待)完成标记为async的方法,其中返回类型为TaskTask<T>(在特殊情况下void )。 async关键字允许在方法/ lambda中使用await关键字。

将lambda标记为异步

class.Method(() => { /* Do something sync*/ };
class.Method(async () => { /* Do something async */ };

在您的情况下,您可以执行类似

的操作
{
    ...
    var client = new RestClient(serviceBaseUrl);
    var request = new RestRequest(serviceUrl, Method.GET);
    var content = await client.ExecuteTaskAsync(request);
    var somethingToReturn = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<List<MyClass>>(content));
    ...
}