什么是使用异步与MVC5的优势在哪里? [英] What is the advantage of using async with MVC5?

查看:807
本文介绍了什么是使用异步与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 code现在有异步,但有什么区别。难道人给比其他的更好的性能?是它容易调试问题与一个比其他?我是否应该更改其他控制器为我的应用程序中添加异步?

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完成端口发出信号,另一个线程从线程池中提取完成返回视图。

  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天全站免登陆