それらは似ていますが同一ではありません。
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
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
/await
イントロが役立つかもしれません。