简体   繁体   中英

Running a scheduled task in an ASP.NET MVC website

I'm in the process of working out the architecture for a website and I've come across something I've never done before with ASP.NET. What I'd like to do is every night run a query against entities in a database table to check a date column and perform either X or Y action, depending on what the date is. Basically I want a function to be called once a night. Everything that I've searched for has lead me in a few different directions that seem either extremely complex for such a simple action, or are leaned more toward job queuing which isn't exactly what I want.

Any suggestions are welcome, just looking for the simplest approach. Thanks!

I've used Hangfire on a project before for doing stuff like this. Its absolutely awesome and keeps all of your code in one solution (no messing around with windows services), I highly recommend.

I can think of three different options.

  1. If the work you're doing is purely database related, most database platforms such as Microsoft SQL Server, Oracle, etc have the ability to schedule jobs that run at regular intervals. These jobs could be SQL statements or stored procedures. MS SQL Server would also let you call C# code from within a stored procedure, which would be handy.

  2. Use the operating system to schedule. Windows has Windows Scheduler jobs (The AT command) and Unix has cron jobs. You could schedule an executable to run every night, which would run C# code or SQL code.

  3. Write your own scheduling service. This service would run all the time as a Windows Service, and execute some code at a regular interval. Check out scheduling frameworks such as Quartz.net , which can help organize some of the scheduling details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM