我执行了以下挂起并从未返回的操作:
public Task<ActionResult> ManageProfile(ManageProfileMessageId? message)
{
ViewBag.StatusMessage =
message == ManageProfileMessageId.ChangeProfileSuccess
? "Your profile has been updated."
: message == ManageProfileMessageId.Error
? "An error has occurred."
: "";
ViewBag.ReturnUrl = Url.Action("ManageProfile");
var user = UserManager.FindByIdAsync(User.Identity.GetUserId());
var profileModel = new UserProfileViewModel
{
Email = user.Email,
City = user.City,
Country = user.Country
};
return View(profileModel);
}
但是当我将其转换为:
public async Task<ActionResult> ManageProfile(ManageProfileMessageId? message)
{
ViewBag.StatusMessage =
message == ManageProfileMessageId.ChangeProfileSuccess
? "Your profile has been updated."
: message == ManageProfileMessageId.Error
? "An error has occurred."
: "";
ViewBag.ReturnUrl = Url.Action("ManageProfile");
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
var profileModel = new UserProfileViewModel
{
Email = user.Email,
City = user.City,
Country = user.Country
};
return View(profileModel);
}
它马上回来了。所以我不确定这是怎么回事?如果返回的方法非常简单,而无需等待FindByIdAsync的结果,那么为什么我没有得到没有任何内容的视图。
因此在我看来,它既没有等待返回:
UserManager.FindByIdAsync(User.Identity.GetUserId());
既不返回空配置文件,也不引发异常。因此,当第一个示例中的内容挂起时,我不知道这里发生了什么。
我认为您的第一个示例正在使用Result
,因此造成了僵局,我在博客上解释了这一僵局。
总之,ASP.NET提供了一个“请求上下文”,一次只能允许一个线程进入。使用阻止线程时Result
,该线程将锁定在该上下文中。稍后,当FindByIdAsync
尝试在该上下文中恢复时,它无法执行,因为其中已经阻塞了另一个线程。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句