在C#.NET MVC应用程序中执行计划的任务 [英] Preforming scheduled tasks in C# .NET MVC Application

查看:62
本文介绍了在C#.NET MVC应用程序中执行计划的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为.NET MVC应用程序实现一种逻辑,该逻辑将在以下基础上触发应用程序中的计划任务:

I'm trying to implement a logic for my .NET MVC application where I would trigger scheduled tasks in my application on a following basis:

First scheduled task to run from 00 am to 1 am in the morning
Second scheduled task to run from 1:10 am to 08:00 am in the morning 
Third scheduled task to run from 8:15 am to 11:15 pm every 1 hour

每年365天每天都会重复一次...

This would repeat every day 365 days a year...

我可以使用.NET或外部库中的哪种机制以很少的代码来实现这种最简单的方法?

Which mechanism in .NET or external library could I use to implement this the easiest way with little code?

我试图用Quartz.NET库实现它,但是我根本不喜欢它,我有两个计划的任务正在运行,而其中一个在计划的时间内根本不会运行...

I was trying to implement it with Quartz.NET library but I didn't like it at all, I had two scheduled tasks running where one would never run at all for the scheduled time...

推荐答案

由于多种原因,您不想在Web应用程序中执行长时间运行且经常发生的后台任务.您可以阅读 following blog post 很好地概述了它们.

You don't want to be performing long running and recurring background tasks in your web application for multiple reasons. You may go through the following blog post which outlines them very well.

我建议您将此逻辑移到单独的Windows服务或控制台应用程序中,该应用程序可以使用Windows Task Scheduler按所需的时间间隔来安排运行.

I would recommend you moving this logic into a separate Windows Service or even a Console Application whose runs could be scheduled with the Windows Task Scheduler at the desired intervals.

这样做的好处是,您将可以减轻Web应用程序的负担,而无需执行长时间运行的任务,这将消耗Web服务器上的宝贵资源.当然,Web服务器和辅助服务都可以共享一个公用的集中式数据存储或消息传递层,以便在必要时进行通信.

The benefit of this is that you will be off-loading your web application from doing long running tasks which will be consuming precious resources from your webserver. Of course both the webserver and the worker service could share a common centralized datastore or messaging layer in order to communicate if necessary.

这篇关于在C#.NET MVC应用程序中执行计划的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆