How to execute a method in Asp.net MVC for every 24 hours

前端 未结 6 1533
时光取名叫无心
时光取名叫无心 2020-12-09 06:32

I have method in MVC like

 public void Sendmails()
 {
     //sending mails for every 24 hours.
 }

Can I schedule above method to execute fo

6条回答
  •  旧巷少年郎
    2020-12-09 07:14

    I agree with @David, running background tasks in web application is inherently not reliable, but there are scenarios, where this makes sense. I'd recommend reading this excellent article first, http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

    With .NET 4.5.2, you can leverage HostingEnvironment, as nicely explains this article: http://blog.mariusschulz.com/2014/05/07/scheduling-background-jobs-from-an-asp-net-application-in-net-4-5-2 Quoting from that, "The HostingEnvironment.QueueBackgroundWorkItem method lets you schedule small background work items. ASP.NET tracks these items and prevents IIS from abruptly terminating the worker process until all background work items have completed."

    But again, it's not a good practice and if used, it should be used quite wisely.

提交回复
热议问题