同一控制器中的异步和同步操作方法

发布于 2024-12-11 14:00:15 字数 269 浏览 0 评论 0原文

AsyncController 中可以有异步和同步操作方法吗?这样做有什么缺点吗?

原因是异步操作对于耗时的操作有意义,但对于返回 HTML 表单没有意义。

例如,我有一个生成文件的页面。返回带有输入的表单的操作方法几乎没有逻辑,但是接收 POST、将其发送到另一个 Web 服务并中继响应的操作方法需要一些时间,因此异步执行此操作是有意义的。

我可以将这些方法放在单独的控制器中,一个同步,另一个同步,但我想将它们放在一起。

干杯。

Is possible to have asynchronous and synchronous action methods in an AsyncController? Is there any drawback doing this?

The reason is that an asynchronous operation makes sense for a time consuming operation, but does not make sense for return a form in HTML.

For example, I have a page that generates a file. The action method that returns the form with the inputs has almost no logic, but the action method that receives the POST, send it to another web service and relay the response takes some time, so it will make sense to do it asynchronously.

I could put those methods in separated controllers, one synchronous and another synchronous, but I would like to keep them together.

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住你手 2024-12-18 14:00:16

AsyncController 中可以有异步和同步操作方法吗?

是的。

这样做有什么缺点吗?

并不真地。

原因是异步操作对于耗时的操作来说是有意义的

耗时的操作 I/O 密集型来说是有意义的,这样您就可以从 I/O 完成端口中受益。如果您有一个在单独线程中运行的耗时 CPU 操作,那么异步控制器绝对不会给您带来任何好处。相反,它会让事情变得更糟。

例如,我有一个生成文件的页面。动作方法
返回带有输入的表单几乎没有逻辑,但是
接收 POST 的操作方法,将其发送到另一个 Web 服务
并转发响应需要一些时间,所以这样做是有意义的
异步。

OK,Web服务调用是一个I/O密集型操作=>您将从异步控制器中受益。在这种情况下,您可以在同一个异步控制器上执行这两个操作。

Is possible to have asynchronous and synchronous action methods in an AsyncController?

Yes.

Is there any drawback doing this?

Not really.

The reason is that an asynchronous operation makes sense for a time consuming operation

time consuming operation and I/O intensive so that you can benefit from I/O Completion Ports. If you have a CPU time consuming operation that you are running in a separate thread you get strictly no benefit from async controllers. At the contrary it will make things worse.

For example, I have a page that generates a file. The action method
that returns the form with the inputs has almost no logic, but the
action method that receives the POST, send it to another web service
and relay the response takes some time, so it will make sense to do it
asynchronously.

OK, web service call is an I/O intensive operation => you will get benefit from an async controller. You could have the two actions in this case on the same async controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文