简体   繁体   English

在ASP.NET MVC网站中运行计划任务

[英]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. 我正在研究一个网站的架构,我遇到过一些我以前从未用过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. 我想做的是每晚对数据库表中的实体运行查询,以检查日期列并执行X或Y操作,具体取决于日期。 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. 我之前在项目上使用了Hangfire来做这样的事情。 Its absolutely awesome and keeps all of your code in one solution (no messing around with windows services), I highly recommend. 它非常棒,并且将所有代码保存在一个解决方案中(不用乱搞Windows服务),我强烈推荐。

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. 如果您正在进行的工作纯粹与数据库相关,那么大多数数据库平台(如Microsoft SQL Server,Oracle等)都可以安排定期运行的作业 These jobs could be SQL statements or stored procedures. 这些作业可以是SQL语句或存储过程。 MS SQL Server would also let you call C# code from within a stored procedure, which would be handy. MS SQL Server还允许您从存储过程中调用C#代码,这将非常方便。

  2. Use the operating system to schedule. 使用操作系统进行计划。 Windows has Windows Scheduler jobs (The AT command) and Unix has cron jobs. Windows有Windows Scheduler作业( AT命令),Unix有cron作业。 You could schedule an executable to run every night, which would run C# code or SQL code. 您可以安排每晚运行一个可执行文件,它将运行C#代码或SQL代码。

  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. 此服务将一直作为Windows服务运行,并定期执行一些代码。 Check out scheduling frameworks such as Quartz.net , which can help organize some of the scheduling details. 查看调度框架,例如Quartz.net ,它可以帮助组织一些调度细节。

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

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