在 MVC5 中使用异步有什么好处? [英] What is the advantage of using async with MVC5?

查看:36
本文介绍了在 MVC5 中使用异步有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别:

public ActionResult Login(LoginViewModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        IdentityResult result = IdentityManager.Authentication.CheckPasswordAndSignIn(AuthenticationManager, model.UserName, model.Password, model.RememberMe);
        if (result.Success)
        {
            return Redirect("~/home");
        }
        else
        {
            AddErrors(result);
        }
    }
    return View(model);
}

和:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        IdentityResult result = await IdentityManager.Authentication.CheckPasswordAndSignInAsync(AuthenticationManager, model.UserName, model.Password, model.RememberMe);
        if (result.Success)
        {
            return Redirect("~/home");
        }
        else
        {
            AddErrors(result);
        }
    }
    return View(model);
}

我看到 MVC 代码现在有 async 但有什么区别.一个比另一个提供更好的性能吗?用一个比另一个更容易调试问题吗?我应该为我的应用程序更改其他控制器以添加 Async 吗?

I see that the MVC code now has async but what is the difference. Does one give much better performance than the other? Is it easier to debug problems with one than the other? Should I make changes to other controllers for my application to add Async ?

推荐答案

异步操作仅在您执行 I/O 绑定操作(例如远程服务器调用)时才有用.异步调用的好处是在 I/O 操作期间,没有使用 ASP.NET 工作线程.下面是第一个示例的工作原理:

The async actions are useful only when you are performing I/O bound operations such as remote server calls. The benefit of the async call is that during the I/O operation, no ASP.NET worker thread is being used. So here's how the first example works:

  1. 当请求命中操作时,ASP.NET 从线程池中取出一个线程并开始执行它.
  2. IdentityManager.Authentication.CheckPasswordAndSignIn 方法被调用.这是一个阻塞调用 -> 在整个调用过程中,工作线程受到威胁.
  1. When a request hits the action, ASP.NET takes a thread from the thread pool and starts executing it.
  2. The IdentityManager.Authentication.CheckPasswordAndSignIn method is invoked. This is a blocking call -> during the entire call the worker thread is being jeopardized.

这里是第二个调用的工作原理:

And here's how the second call works:

  1. 当请求命中操作时,ASP.NET 从线程池中取出一个线程并开始执行它.
  2. 调用立即返回的 IdentityManager.Authentication.CheckPasswordAndSignInAsync.注册 I/O 完成端口并将 ASP.NET 工作线程释放到线程池.
  3. 稍后当操作完成时,I/O Completion 端口会发出信号,从线程池中抽取另一个线程以完成返回视图.
  1. When a request hits the action, ASP.NET takes a thread from the thread pool and starts executing it.
  2. The IdentityManager.Authentication.CheckPasswordAndSignInAsync is called which returns immediately. An I/O Completion Port is registered and the ASP.NET worker thread is released to the thread pool.
  3. Later when the operation completes, the I/O Completion port is signaled, another thread is drawn from the thread pool to finish returning the view.

正如您在第二种情况下看到的,ASP.NET 工作线程仅使用很短的时间.这意味着池中有更多线程可用于处理其他请求.

As you can see in the second case ASP.NET worker threads are used only for a short period of time. This means that there are more threads available in the pool for serving other requests.

总而言之,只有当您拥有真正的异步 API 时才使用异步操作.如果您在异步操作中进行阻塞调用,您将失去它的全部好处.

So to conclude, use async actions only when you have a true async API inside. If you make a blocking call inside an async action, you are killing the whole benefit of it.

这篇关于在 MVC5 中使用异步有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆