C# 在ASP.NETMVC中使用异步
我做了以下事情,但一直没有回来:C# 在ASP.NETMVC中使用异步,c#,asp.net-mvc,async-await,C#,Asp.net Mvc,Async Await,我做了以下事情,但一直没有回来: public Task<ActionResult> ManageProfile(ManageProfileMessageId? message) { ViewBag.StatusMessage = message == ManageProfileMessageId.ChangeProfileSuccess ? "Your profile h
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);
}
也没有返回空配置文件,也没有引发异常。因此,当它挂起在第一个示例中时,我不知道这里发生了什么。我假设您的第一个示例使用的是
结果,因此
总之,ASP.NET提供了一个“请求上下文”,一次只允许一个线程进入。当您使用Result
阻止线程时,该线程将锁定到该上下文中。稍后,当FindByIdAsync
尝试在该上下文上恢复时,它无法恢复,因为其中已阻止了另一个线程。您的第一个代码段未编译。您的第二个代码段中没有错误,问题很可能出在UserManager.FindByIdAsync
方法上,我们没有相应的代码。如果我猜的话,我会说它没有启动它返回的任务,所以任务被无限期地等待。按照惯例,异步方法应该总是启动它返回的任务。谢谢你的博客文章。正是我需要的。
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);
}
UserManager.FindByIdAsync(User.Identity.GetUserId());