我正在try 理解为什么以及何时应该使用async控制器操作.最后,当我在其中使用await时,它将等待操作完成,以便返回视图.

例如

public async Task<ActionResult> TryMe()
{
   await SomeActionAsync();
   return View();
}

在这种情况下,如果我使用async或不使用asyncAction将花费相同的时间执行.

如果我没有try 并行运行至少两个慢操作(互不依赖),我看不出有任何理由使用async控制器操作.

如果我说错了,请纠正我.我想我遗漏了什么.

推荐答案

await关键字的目的是让你不用写难看的回调就可以使用asynchronous operations.

使用异步操作有助于避免浪费线程池线程.

Explanation

ASP.Net runs all of your code in threads from the managed thread pool.
If you have too many slow requests running at once, the thread pool will get full, and new requests will need to wait for a thread to get free.

然而,通常情况下,您的请求之所以慢,并不是因为它们正在进行计算(计算绑定),而是因为它们正在等待其他东西,例如硬盘、数据库服务器或外部Web服务(IO或网络绑定).

仅仅为了等待外部操作完成而浪费宝贵的线程池线程是没有意义的.

Asynchronous operations allow you to start the operation, return your thread to the pool, then "wake up" on a different thread pool thread when the operation is finished.
While the operation is running, no threads are consumed.

Asp.net相关问答推荐

Visual Studio发布的网站得到错误类型JObject is not defined when page is load on server"''"

域名 + 子域名在IIS部署

授权错误错误 400:C# 上的 redirect_uri_mismatch

当前http上下文的http动词

Window.Open 使用 PDF 流而不是 PDF 位置

模型项的类型为 CookMeIndexViewModel,但需要类型为 IEnumerable 的模型项

如何获取 ActionLink 的工具提示?

如何使用 asp.net 获取 html Select 的选定值

MVC5 身份验证中的......与主域之间的信任关系失败

如何在 ASP.NET Core Web API 中配置 JSON 格式的缩进

如何使用 WebRequest 发布数据并从网页获取响应

如何将表从存储过程检索到数据表?

ASP.NET MVC Url Route 支持(点)

无法在 IIS 中启动网站 - W3SVC 正在运行

如何将参数传递给 ASP.NET MVC 2 中的自定义 ActionFilter?

在 Application_BeginRequest 中设置会话变量

有没有办法在没有异常类的情况下抛出自定义异常

当用户在文本框中按 Enter 键时执行按钮单击事件

人们使用 JScript.Net 的目的是什么?

ASP.NET 自定义控件 - 未知的服务器标记