我有一个标准格式,用Html.BeginForm发布到控制器中的异步操作。它看起来像这样(这是一个轮廓,而不是实际的代码):ASP.NET MVC POST第一次尝试无限期等待
[HttpPost]
public async Task<ActionResult> Index(UserCreds creds)
{
try
{
if (ModelState.IsValid)
{
var user = await loginRep.Login(creds.Username, creds.Password);
if (user != null)
{
_context.SetAuthenticationToken(user);
return RedirectToAction("Index", "Landing");
}
else
{
ModelState.AddModelError("", "Login failed.");
}
}
else
{
_logger.Debug(string.Format("User failed authentication."));
}
}
catch (Exception ex)
{
throw new HttpException(500, string.Format("An error occured during the execution of the action {0} from Controller {1}", "Index", "Login"), ex);
}
return View();
}
在提交表单的第一次,浏览器将是一个响应无限期地等待,尽管可以看到与步进调试器到达RedirectToAction。如果在浏览器中停止请求,并且只需再次提交,就会发生重定向。所有后续尝试登录也会成功重定向。在第一次尝试期间,身份验证令牌也不会被设置。
这可能与loginRep.Login
中的委托使用有关。它内部最终做这样的事情:
private async Task<LoginResponse> SendLoginRequest(string username, string password)
{
TaskCompletionSource<LoginResponse> tcs = new TaskCompletionSource<LoginResponse>();
LoginResponseCallback callback = null;
callback = new LoginResponseHandler(delegate (response) {
securityService.OnLoginResponse -= callback;
tcs.SetResult(response);
});
securityService.OnLoginResponse += callback;
securityService.SendLoginRequest(username, password);
return await tcs.Task;
}
有没有人明白发生了什么?如果它是一个死锁,我不会期望看到调试器达到重定向,我也不会期望登录工作的所有尝试以外的第一次。
请注意,如果只是跳过发送登录请求并硬编码成功响应的样子,表单会首次运行。
是否有什么(可能是设置提供程序或类似的),只在第一次调用时被初始化,这对你的异步方法来说不是很好? – Porschiey
不确定。但是,登录响应第一次起作用。我忘了提及该项目最初是MVC 2.0,然后被迁移到MVC 5.0。我一直在想,也许我应该删除Web.config,然后重新创建它,以排除旧配置的干扰。让我尝试一下。 –
制作一个新的Web.config证明太耗时。是否将运行时信息添加到当前的Web.config中,但问题仍然存在。 –