0

我正在学习 ASP.NET MVC 中的 AsyncController 并将其与 TPL 一起使用,但我很难看到它的需求,我可以理解您何时想要异步运行 Action 来执行诸如发送电子邮件之类的操作,但实际上你会用它从一个动作中返回一个视图吗?

例如,如果 Action 从设置为异步工作的数据库中获取一些数据,然后返回一个 View,如果数据未能及时检索,View 会不会只是返回模型中没有数据?

4

2 回答 2

3

你会用它从一个动作中返回一个视图吗?

ASP.NET 中异步的主要优点是可伸缩性。在执行异步工作时,您不会消耗任何线程。这意味着您的应用程序将消耗更少的内存,并且它也可能更快。

如果数据未能及时检索,视图是否会在模型中没有数据的情况下返回?

这取决于您以及您将如何处理该故障。

于 2013-03-29T22:08:39.573 回答
2

异步控制器主要用于放弃当前线程池线程,以允许其他传入连接在您等待长时间运行的进程完成时处理工作。

这与传回视图无关。从最终用户的角度来看,该过程仍将“阻塞”,但在服务器上,服务器响应传入请求所需的资源将不会被消耗。

By default, there are 250 thread pool threads per cpu core in an IIS worker process to respond to incoming connections (this can be tuned, but in general you should know what you're doing). If you have to people waiting for long requests to complete, then nobody else will be able to connect to your server until one of them finishes. Async controllers fix that problem.

You can also offload CPU bound work to a dedicated thread when using async controllers, where that was more difficult in synchronous controllers. And, it allows you to perform tasks in parallel. For instance, suppose you have to go out to 10 web sites and retrieve data. Most of the time is spent waiting for those web requests to return, and they can be done in parallel if you are doing things async.

于 2013-03-29T22:19:08.190 回答