简体   繁体   English

没有 IHostedService 运行应用程序的整个生命周期的长时间运行的任务?

[英]Long-running task without IHostedService running the entire life of the application?

I have a website page that needs the option of performing an operation that could take several minutes.我有一个网站页面,需要选择执行可能需要几分钟的操作。 To avoid performance issues and time outs, I want to run this operation outside of the HTTP request.为避免性能问题和超时,我想在 HTTP 请求之外运行此操作。

After some research, I found IHostedService and BackgroundService , which can be registered as a singleton using AddHostedService<T>() .经过一些研究,我找到IHostedServiceBackgroundService ,它们可以使用AddHostedService<T>()注册为 singleton 。

But my concern is that a hosted service is always running.但我担心的是托管服务始终在运行。 Doesn't that seem like a waste of resources when I just want it to run on demand?当我只希望它按需运行时,这不是浪费资源吗?

Does anyone know a better option to run a lengthy task, or a way to use IHostedService that doesn't need to run endlessly?有谁知道运行冗长任务的更好选择,或者使用不需要无休止运行的IHostedService的方法?

Note that the operation calls and waits for an API call.请注意,该操作调用并等待 API 调用。 And so I cannot report the progress of the operation, nor can I set a flag in a common database regarding whether the operation has completed.因此我无法报告操作的进度,也无法在公共数据库中设置标志以判断操作是否已完成。

One option to run a lengthy task on demand while avoiding performance issues and time outs is to use a message queue .按需运行冗长任务同时避免性能问题和超时的一种选择是使用message queue You can have your Razor Pages website send a message to the queue when the operation is requested, and have a separate service, such as a background worker , consume messages from the queue and perform the operation.您可以让您的 Razor Pages 网站在请求操作时向队列发送消息,并让单独的服务(例如后台工作人员)使用队列中的消息并执行操作。 This allows you to decouple the task from the web request, and also allows for the possibility of adding more worker instances to handle the workload.这允许您将任务与 web 请求分离,并且还允许添加更多工作实例来处理工作负载的可能性。

Another option is to use a task scheduler that runs on demand, such as Hangfire .另一种选择是使用按需运行的task scheduler ,例如Hangfire It allows you to schedule background jobs and monitor their progress, which can be useful in your scenario where you cannot report the progress of the operation.它允许您安排后台作业并监视它们的进度,这在您无法报告操作进度的情况下非常有用。

You can also use IHostedService , but you need to make sure that the service is only running when it is needed.您也可以使用IHostedService ,但您需要确保该服务仅在需要时运行。 You can use a flag or a semaphore to control whether the service is running or not.您可以使用flagsemaphore来控制服务是否正在运行。 You can set the flag or semaphore when the operation is requested, and clear it when the operation is completed.您可以在请求操作时设置标志或信号量,并在操作完成时将其清除。 The service can then check the flag or semaphore in its main loop, and exit if the flag is not set.然后服务可以在其主循环中检查标志或信号量,如果未设置标志则退出。

In summary:总之:

message queue , task scheduler , and IHostedService with controlling flag/semaphore are all viable options for running a lengthy task on demand. message queuetask scheduler和带有控制flag/semaphoreIHostedService都是按需运行冗长任务的可行选项。 The best option depends on your specific use case and requirements.最佳选择取决于您的具体用例和要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rx如何并行化长时间运行的任务? - Rx How to parallelize long-running task? 长期运行的后台任务中的Internet访问 - Internet access in a long-running background task 在Windows服务中运行长时间运行的任务 - Running a long-running Task within a Windows Service Windows Service Application仅执行一次长时间运行的任务,并在完成后自行停止 - Windows Service Application to perform long-running task only once and stop itself after completion 在长时间运行的任务中显示自定义对话框窗口 - Displaying a custom dialog window during a long-running task 长期运行任务与线程 - 性能 - Long-running task vs. threads — performance MVVM-无需异步等待即可长时间运行的操作 - MVVM - long-running operation without async-await WinForm 应用程序 UI 在长时间运行期间挂起 - WinForm Application UI Hangs during Long-Running Operation MEF 出口工厂<t> - 如何在长时间运行的应用程序中正确处理?</t> - MEF ExportFactory<T> - How to properly dispose in a long-running application? 长时间运行的异步操作,无IO线程 - Long-Running Asynchronous Operations, Without IO threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM