我在Azure有一个ASP.NET WebApi。它上没有基于HTTP报头或Azure API的身份验证,而fiddler会话证明该API是完全功能的,并按请求和预期输出数据。
在我的Xamarin (仅限iOS和Android ) PCL项目中,服务类中有以下代码:
public async Task<IEnumerable<Link>> GetCategories()
{
IEnumerable<Link> categoryLinks = null;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Using https to satisfy iOS ATS requirements
var response = await client.GetAsync(new Uri("https://myproject.azurewebsites.net/api/contents"));
//response.EnsureSuccessStatusCode(); //I was playing around with this to see if it makes any difference
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().Result;
categoryLinks = JsonConvert.DeserializeObject<IEnumerable<Link>>(content);
}
}
return categoryLinks;
}我已经调试了代码,并注意到控件没有经过:
var response = await client.GetAsync(new Uri("https://myproject.azurewebsites.net/api/contents"));因此,categoryLinks仍然是空的。
以前有人遇到过这种情况吗?
有几件事要注意:
如能在这方面提供任何协助,将不胜感激。
发布于 2016-07-11 05:38:26
我遇到了这个问题,它是由死锁引起的,您是如何调用此方法的?通常我会去:
Device.BeginInvokeInMainThread(async () =>
{
var categoriesList = await GetCategories();
});或者如果它在您的视图模型中,您可以使用Task.Run();
避免使用.Result,而UI中的计时器--甚至事件处理程序--也可能导致类似于此的死锁。
希望这能有所帮助。
https://stackoverflow.com/questions/38291977
复制相似问题