更新时间:2023-02-25 08:33:57
异步操作仅当您正在执行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:
IdentityManager.Authentication.CheckPasswordAndSignIn
方法被调用。这是一个阻塞调用 - 整个呼叫工作线程被破坏时> 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:
IdentityManager.Authentication.CheckPasswordAndSignInAsync
称为其立即返回。一个I / O完成端口被注册和ASP.NET辅助线程释放到线程池。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.你可以在第二种情况下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.