【发布时间】:2016-11-30 02:11:07
【问题描述】:
我有一个从 API 检索数据的函数(如下)。如果我在反序列化它的行设置断点,那么我可以看到它填充了很棒的数据。
当我继续时,它进入第二个函数(如下)并引发错误。错误旁边写着Not yet computed,因此抛出异常。
当我使用一个小列表时,它工作得很好(我认为它是一小组数据)。
当我使用ContinueWith(等待任务完成)时,这怎么可能?
public static async Task<Data> GetAllCardsInSet(string setName)
{
setName = WebUtility.UrlEncode(setName);
var correctUri = Path.Combine(ApiConstants.YugiohGetAllCardsInSet, setName);
Console.WriteLine();
using (var httpClient = new HttpClient())
{
var response =
await httpClient.GetAsync(correctUri);
var result = await response.Content.ReadAsStringAsync();
var cardData = JsonConvert.DeserializeObject<CardSetCards>(result);
for (int i = 0; i < cardData.Data.Cards.Count; i++)
{
cardData.Data.Cards[i] = FormatWords(cardData.Data.Cards[i]);
}
return cardData.Data;
}
}
private void GetYugiohCardsAndNavigate(string name)
{
var cardSetData = YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name).ContinueWith((result) =>
{
//var cards = await YugiohRequester.GetAllCardsInSet(_selectedCardSet.Name);
try
{
this.mainPage.NavigateToYugiohCardListPage(result.Result);
}
catch (Exception e)
{
HelperFunctions.ShowToastNotification("Trading Card App", "Sorry, we could not fetch this set");
}
});
}
【问题讨论】:
-
如果你避免
ContinueWith只是等待GetAllCardsInSet方法会发生什么? -
给我一个空引用异常
-
你能调查一下如果你返回
await Task.FromResult(cardData.Data)而不是cardData.Data会发生什么。 -
您的
GetAllCardsInSet正在引发异常。检查 ContinueWith 块中的IsFaultedresult参数,查看GetAllCardsInSet是否抛出异常 -
@magna_nz:永远不要使用
ContinueWith。请改用await。
标签: c# .net asynchronous async-await