asp.net-mvc 在 MVC5 中使用异步有什么好处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19087513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:11:18  来源:igfitidea点击:

What is the advantage of using async with MVC5?

asp.net-mvctask-parallel-libraryasync-awaitasp.net-mvc-5asp.net-identity

提问by Darin Dimitrov

What is the difference between:

有什么区别:

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);
}

and:

和:

[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);
}

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 ?

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

回答by Darin Dimitrov

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:

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

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

And here's how the second call works:

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

  1. When a request hits the action, ASP.NET takes a thread from the thread pool and starts executing it.
  2. The IdentityManager.Authentication.CheckPasswordAndSignInAsyncis 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.
  1. 当请求命中操作时,ASP.NET 从线程池中取出一个线程并开始执行它。
  2. IdentityManager.Authentication.CheckPasswordAndSignInAsync立即返回的被调用。注册 I/O 完成端口,并将 ASP.NET 工作线程释放到线程池。
  3. 稍后当操作完成时,I/O Completion 端口被发出信号,从线程池中抽取另一个线程以完成返回视图。

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.

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

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.

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

回答by Gaurang Dhandhukiya

Normally, a single HTTP request would be handled by a single thread, completely removing that thread from the pool until a response is returned. With the TPL, you are not bound by this constraint. Any request that come in starts a continuation with each unit of computation required to calculate a response able to execute on any thread in the pool. With this model, you can handle many more concurrent requests than with standard ASP.Net.

通常,单个 HTTP 请求将由单个线程处理,从池中完全删除该线程,直到返回响应。使用 TPL,您不受此约束的约束。任何进入的请求都会开始一个延续,每个计算单元都需要计算能够在池中的任何线程上执行的响应。使用此模型,您可以处理比标准 ASP.Net 多得多的并发请求。

If it is some new task that will be spawned, or not, and if it should be awaited or not. Always think about those 70 ms, which is approx. the max. time that any method call should take. If its longer, then your UI will most probably not feel very responsiveness.

是否会产生一些新任务,以及是否应该等待。总是考虑那些 70 毫秒,大约是 70 毫秒。最大。任何方法调用应该花费的时间。如果它更长,那么您的 UI 很可能不会感觉非常灵敏。

回答by Muhammad Essa

In web applications that sees a large number of concurrent requests at start-up or has a bursty load (where concurrency increases suddenly), making these web service calls asynchronous will increase the responsiveness of your application. An asynchronous request takes the same amount of time to process as a synchronous request. For example, if a request makes a web service call that requires two seconds to complete, the request takes two seconds whether it is performed synchronously or asynchronously. However, during an asynchronous call, a thread is not blocked from responding to other requests while it waits for the first request to complete. Therefore, asynchronous requests prevent request queuing and thread pool growth when there are many concurrent requests that invoke long-running operations.

在启动时看到大量并发请求或具有突发负载(并发性突然增加)的 Web 应用程序中,使这些 Web 服务调用异步将提高应用程序的响应能力。异步请求的处理时间与同步请求的处理时间相同。例如,如果请求进行需要两秒钟才能完成的 Web 服务调用,则无论是同步执行还是异步执行,该请求都需要两秒钟。但是,在异步调用期间,线程在等待第一个请求完成时不会被阻止响应其他请求。因此,当有许多并发请求调用长时间运行的操作时,异步请求会阻止请求排队和线程池增长。