这些相似但不相同。
ContinueWith返回一个Task表示延续的。所以,举个例子:
JsonValue json = await response.Content.ReadAsStringAsync()
.ContinueWith<JsonValue>(respTask => JsonValue.Parse(respTask.Result));
只考虑表达式:
response.Content.ReadAsStringAsync()
.ContinueWith<JsonValue>(respTask => JsonValue.Parse(respTask.Result));
此表达式的结果是Task表示由 安排的延续ContinueWith。
所以,当你await表达:
await response.Content.ReadAsStringAsync()
.ContinueWith<JsonValue>(respTask => JsonValue.Parse(respTask.Result));
您确实await正在Task返回 by ContinueWith,并且在继续完成json之前不会对变量进行赋值ContinueWith:
JsonValue json = await response.Content.ReadAsStringAsync()
.ContinueWith<JsonValue>(respTask => JsonValue.Parse(respTask.Result));
一般来说,我ContinueWith在编写async代码时会避免。没啥毛病,就是有点低级,语法也比较别扭。
在你的情况下,我会做这样的事情:
var responseValue = await response.Content.ReadAsStringAsync();
var json = JsonValue.Parse(responseValue);
如果这是数据访问层的一部分,我也会使用ConfigureAwait(false)它,但由于您response.Content直接访问,我假设您稍后在此方法中需要 ASP.NET 上下文。
由于您是async/新手await,您可能会发现我的async/ awaitintro很有帮助。