ASP.NET (VB) 异步执行长函数而不阻塞页面线程?
我一直在阅读 ASP.NET 3.5 的异步函数/网页(我们还没有使用 v4),但我看到的那些都专注于执行后台任务,但仍然回来完成并发送任务完成后的响应。
我想要做的只是启动后台任务(在本例中是对 Web 服务的调用),然后立即向浏览器返回响应 - 即。 无需等待异步任务完成。我什至不需要知道它是否成功。
最好的方法是什么?我似乎找不到在 ASP.NET 中启动所谓的“孤立”后台任务的示例。
我想通过在向用户显示的页面上进行 javascript Ajax 调用来完成此操作,但传递到 Web 服务的信息是敏感的,所以这是不可能的。但这说明了我想做的事情。
[ed] 另一个想法:在 ASP.NET 模型中是否有一个我可以使用的事件,该事件发生在将响应发送到浏览器并关闭连接之后? IE。那么更多的处理可以在用户不等待的情况下进行吗?
I've been reading up on asynchronous function / web pages for ASP.NET 3.5 (we're not using v4 yet) but the ones I've seen all focus on performing a background task but then still coming back to finish up and send the response, after the task is complete.
What I want to do is simply kick off the background task (in this case a call to a web service) but then return a response to the browser immediately - ie. without waiting for the asynchronous task to complete. I don't even need to know if it succeeds or not.
What is the best way to do that? I can't seem to find examples of kicking off what you might call an "orphan" background task in ASP.NET.
I thought of doing it via a javascript Ajax call on the page I show to the user, but the information passed to the web service is sensitive, so that's out of the question. But that kind of illustrates what I want to do.
[ed] Another idea: Is there an event in the ASP.NET model I can use which occurs after the response has been sent to the browser and the connection closed? ie. So more processing can occur without the user waiting for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用线程...如果您不关心响应,只需使用:
You can use threads...if you don't care about the response, just use:
我已经对这个主题进行了相当多的探索,但尚未在 ASP.Net 中找到真正异步的出色答案或技术。问题似乎是,即使您使用下面列出的任何流行技术启动异步任务:
您仍然会造成一种情况,即您的网络应用程序可用的有限线程被阻止。 UI 可能不会出现阻塞,但“ThreadPool”中的另一个线程肯定会阻塞。当每个线程都需要快速提供请求时,这可能会导致您的应用程序在高流量期间出现阻塞。
尽管这是额外的工作,并且会有一系列维护问题。我建议将所有长时间运行的任务卸载到 MSMQ 队列,然后利用 Windows 服务来处理队列上的任务。这将允许您的网络应用程序利用所有可用的线程来提供请求,并且您卸载的任务将继续在后台处理。使用 MSMQ 的另一个好处是您实际上可以将任务发送到完全不同的服务器。这有两个好处:
正如我所说,我知道这是一堆额外的工作,但如果您有一个高性能的网站,那么这将是一个更安全的选择。目标是让 Web 服务器尽可能平稳地运行,而不会因长时间运行的任务而陷入困境。
I have explored this topic quite a bit and have not found a great answer or technique within ASP.Net that is truly asynchronous. The problem seems to be that even if you kick off an Async task using any of the popular techniques listed below:
You are still creating a situation where the limited threads available to your web app are being blocked. The UI may not appear blocked but another thread in the "ThreadPool" definitely is. This can cause your application to choke up during periods of high traffic when every thread needs to be serving up request quickly.
Although it is extra work and will have its own set of maintenance issues. I would recommend offloading any long running tasks to an MSMQ queue and then utilize a Windows Service to process tasks on the queue. This will allow you web app to utilize all available threads for serving up requests and the tasks you offloaded will keep getting processed in the background. Another benefit of using MSMQ is that you can actually send the tasks to a completely different server. This has 2 benefits:
As I said, I know this is a bunch of extra work but if you have a high performing site then this would be a safer bet. The goal is to keep the web server running as smoothly as possible without bogging it down with long running tasks.