Task.ContinueWith()未按预期执行

时间:2014-08-27 09:12:30

标签: c# asynchronous task continuations

我尝试使用async / await重写一些旧代码并使用ContinueWith()链接任务并使用TaskContinuationOptions.NotOnFaulted检查异常。

当我调试代码时,我注意到它没有像我预期的那样运行。 两个webrequests都是成功的,但只有第一个继续处理响应。

第二个延续没有完成 最后一个给了我结果:

Id = 1, Status = RanToCompletion, Method = "{null}", Result = "System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult]"

结果:

Id = 2, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

我的问题是我做错了什么,我该怎么做才能完成第二次延续。 如果使用ContinueWith将任务链接在一起或者如果有更好的方法可以在不编写一堆笨拙的方法的情况下将任务链接在一起,我也感兴趣吗? 谢谢你的帮助

   using Newtonsoft.Json.Linq;                  

   var api = new Api();
   var order = new Dictionary<string, object>();
   await api.MakeRequest(Api.Endpoint.Orders, HttpMethod.Get, null, "?completed=false&page=" + count)
   //Look for new Orders
   .ContinueWith(ant =>
    {
          dynamic jsonOrder = JObject.Parse(ant.Result);
          JArray data = jsonOrder.data;
          //Process Json Response
          order.Add("customer_name", (string)data[j]["customer_name"]);
          order.Add("product_id", (string)data[j]["product_id"]);
          order.Add("order_id", (string)data[j]["order_id"]);
          order.Add("timestamp", (int)data[j]["timestamp"]);
          //Entries are successfully added
    }, TaskContinuationOptions.NotOnFaulted )
    //Now get more details about the product
    .ContinueWith(async (ant) =>
    {
          string result = await api.MakeRequest(Api.Endpoint.Product, HttpMethod.Get, null, (string)order["product_id"]); 
          //The Request succeeds 

          //This code block does not execute
          dynamic json = JObject.Parse(result);
          order.Add("deadline", (int)json.data.deadline);
          order.Add("price", (string)json.data.price);
          order.Add("amount", (int)json.data.amount);
          //This code block does not execute 

     }, TaskContinuationOptions.NotOnFaulted)
     //Get some more details about the Customer (isRecurring? etc)
     .ContinueWith(async (ant) =>
     {
        //Some more code here
     }

1 个答案:

答案 0 :(得分:1)

与@Ben一样,Robinson说使用await会自动将方法的其余部分注册为延续,只有在操作成功时才​​会执行,否则会抛出异常。我将更改我的方法以删除ContinueWith调用,如果在异步操作完成后不需要返回当前ConfigureAwait(false),则考虑使用SynchrionizationContext,即其余的该方法将继续在线程池线程上执行。您可能还会发现this article有用。

var api = new Api();
var order = new Dictionary<string, object>();

await api.MakeRequest(Api.Endpoint.Orders, HttpMethod.Get, null, "?completed=false&page=" + count).ConfiugureAwait(false);

//Look for new Orders
dynamic jsonOrder = JObject.Parse(ant.Result);
JArray data = jsonOrder.data;
//Process Json Response
order.Add("customer_name", (string)data[j]["customer_name"]);
order.Add("product_id", (string)data[j]["product_id"]);
order.Add("order_id", (string)data[j]["order_id"]);
order.Add("timestamp", (int)data[j]["timestamp"]);

//Now get more details about the product
string result = await api.MakeRequest(Api.Endpoint.Product, HttpMethod.Get, null, (string)order["product_id"]).ConfiugureAwait(false);

dynamic json = JObject.Parse(result);
order.Add("deadline", (int)json.data.deadline);
order.Add("price", (string)json.data.price);
order.Add("amount", (int)json.data.amount);