Launching a long-running background task - from ASP.NET or a scheduled job

落爺英雄遲暮 提交于 2019-12-04 16:27:22

First off - for several reasons - ASP.NET is imho not the solution for long-running tasks/jobs/... whatsoever.

I have had this requirement a lot of times, and always solved/separated it like:

Worker
A service with

  • Quartz.net (for scheduling and processing, even if you don't have a specific timestamp for execution - but the overall handling in this framework is simply superb)
  • a persistent job-store (to handle start/stop and completed/aborted/paused jobs)
  • eg ServiceStack as the interop between the two processes

Website
Simply calls some webservice-methods of the worker to enqueue/query/pause/stop/... a job. For querying jobs a call to a unified job-store might be an option (eg. db)

It might be a bit of an overkill for you though ... but this is my Swiss army knife for such scenarios.

Hangfire is what you are looking for. Best part is it comes with a built in dashboard.

You might have to write some logic on the top of it.

You can find it here. http://hangfire.io/

Use the standard built-in Windows Task Scheduler like you have done, but invoke it from your web application.

Configure your task in Task Scheduler. It does not need to have a scheduled trigger. From your web application, just use Process.Start to kick it off:

SchTasks.exe /Run /TN Folder\Taskname

I have not used SchTasks.exe directly, but have used the Microsoft.Win32.TaskScheduler wrapper classes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!