0

I'd like to set up a function using a Timer (or some similar approach) that would allow a C# MVC application to do a certain task (in my case, I want to go through database and update specific records every 5 minutes). I have the code I want to be executed, but I don't know how to set it up to be executed every 5 minutes regardless of whether someone is using the application or not (basically I want the task to start running as soon as application is deployed and to never stop (not really never, but when application is erased from server)).

I'm currently using Shared Windows Hosting, so can't access Task Scheduler.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Alen Šimunic
  • 555
  • 1
  • 7
  • 19
  • 3
    short answer: you can't and you shouldn't. your app pool might get killed by IIS anytime and there is no guarantee that your task will run to the end or if it will ever resume after getting killed – Steve Jul 13 '18 at 16:51
  • 3
    use windows Task Scheduler or Windows Service instead – Steve Jul 13 '18 at 16:52
  • @Steve updated reason why I can't user Task Scheduler. Is Windows Service accessible on Shared Hosting? – Alen Šimunic Jul 13 '18 at 16:53
  • 1
    Use a Azure webjob, setting a cron expression for every 5 minutes – Krum Bagashev Jul 13 '18 at 16:54
  • @KrumBagashev Why do you assume the OP has access to or wants to use Azure? – Camilo Terevinto Jul 13 '18 at 16:54
  • @KrumBagashev Seems like an option, but I if possible, I'd avoid Azure and stay on my Shared Hosting account, but if not possible, Azure seems just fine – Alen Šimunic Jul 13 '18 at 16:56
  • @AlenŠimunic in such case you need to have detection logic to restart the task. Something has to constantly pinging the url to keep your app pool alive. your task has to be able to be able to live thru force kill (not screw things up). Otherwise just spend more $$ and get a VPS. The $ you saved from hacking up a solution for shared host is not worth the time IMO – Steve Jul 13 '18 at 16:58
  • @CamiloTerevinto You can always add Authentication or whitelist with a firewall rule only to be visible to the Internal Infrastructure – Krum Bagashev Jul 13 '18 at 16:59
  • @Steve Thank you, I now fully understand that an additional service should be taken (Azure timer functions seems like a feasible solution) – Alen Šimunic Jul 13 '18 at 17:00

1 Answers1

1

Web applications hosted within IIS or other web servers are generally not designed to run timed or long running tasks - your task could be killed anytime by the web server. Offload this work to Windows Task Scheduler, a Windows Service, or timed Azure Function/AWS Lambda (if in cloud)

Cam Bruce
  • 5,632
  • 19
  • 34