C# HttpClient GetAsync冻结
授权后,将加载联系人页面。在viewmodel页面中调用该方法C# HttpClient GetAsync冻结,c#,asp.net,xamarin,xamarin.forms,C#,Asp.net,Xamarin,Xamarin.forms,授权后,将加载联系人页面。在viewmodel页面中调用该方法 public partial class Contacts : ContentPage { ContactsPageViewModels vm; public Contacts() { vm = new ContactsPageViewModels(); BindingCon
public partial class Contacts : ContentPage
{
ContactsPageViewModels vm;
public Contacts()
{
vm = new ContactsPageViewModels();
BindingContext = vm;
InitializeComponent();
}
}
我试着只发送200行。所有东西都会进入http分析器,但在应用程序本身中,它挂在第一行
public async Task<ObservableCollection<UserModel>> GetContactsList()
{
//freezes in the first line
var response = await client.GetAsync("http://localhost:52059/api/Home/GetContacts/" + Convert.ToString(App.User.ID));
string responseBody = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<ObservableCollection<UserModel>>(responseBody);
}
控制器
[HttpGet]
[Route("GetContacts/{id}")]
public ActionResult GetContacts(int id)
{
ObservableCollection<UserModel> users = new ObservableCollection<UserModel>();
foreach (UserModel user in db.UserModels)
users.Add(user);
users.Remove(db.UserModels.FirstOrDefault(u => u.ID == id));
Response.Headers.Add("Content-Type", "application/json");
//return Ok();
return new JsonResult(users);
}
在postman all work中这听起来像是同步上下文和应用程序主线程上的问题 请在此处阅读更多信息: 或在此: 并尝试:
public async Task<ObservableCollection<UserModel>> GetContactsList()
{
//freezes in the first line
var response = await client.GetAsync("http://localhost:52059/api/Home/GetContacts/" + Convert.ToString(App.User.ID)).ConfigureAwait(false);
string responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<ObservableCollection<UserModel>>(responseBody);
}
发生的是死锁,因为主线程正在等待异步结束。async希望返回到线程,但不能,因为主线程正在等待它
.configurewaitfalse
将说不深入研究状态机以及async/await如何工作,它将在其他线程上结束,调用主线程/同步线程。它无限期挂起,还是在某个点继续?绞刑到底是什么意思?冻结到底是什么意思?Xanarin应用程序?它在哪里结冰?您如何调用GetContactsList?您是否一直在执行异步?何时执行GetContactsList方法?在ViewModel的ctor中,或者?