在C#.NET Web应用程序中每天安排任务的正确方法 [英] Proper way to schedule a task daily in C# .NET web application

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

问题描述

我有一个ASP.NET MVC应用程序,可让用户存储视频和照片等媒体.

I have an ASP.NET MVC app that let users store media like videos and photos.

该应用程序还允许用户删除存储在服务器中的那些媒体.要做到这一点我想实现一个两步"的过程中,第一个(当选择媒体被删除),在该数据库中的媒体项目将得到一个时间戳表明该条目在一段时间内被删除(例如一个月);如果没有在那个时间删除该时间戳记,则应该有一个计划的方法每天执行一次,并检查媒体条目是否具有足以删除的时间戳记.

The application also lets the users delete those media stored in the server. To do so i want to implement a "two steps" process in which first (when the media is selected to be deleted), the media entry at the database will get a timestamp that indicates that the entry has to be deleted in some time (a month for example); if that timestamp is not removed in that time, there should be a scheduled method that executes daily and checks if a media entry has a timestamp old enough to be deleted.

我的问题是关于调度程序的:我已经读过,有一些诸如FluentScheduler或Quartz.NET之类的框架可以实现这种工作.由于该应用程序是在IIS中部署的Web应用程序,因此恐怕没有使用正确的方法,框架或方法来实现它.由于IIS可以随时关闭我的应用程序,并且由于计划的方法将执行对DB和IO File系统的写入操作以删除db条目和物理文件,因此对我来说至关重要的是,该方法必须完全执行或不在以下位置执行全部,以使数据库与高清存储的文件保持一致.

My question is about the scheduler: I've readed that there are some frameworks like FluentScheduler or Quartz.NET to implement that kind of jobs. Since the application is a web application deployed in IIS, i'm afraid to not use the proper way, framework or method to implement it. Since IIS could shutdown my application at any time and since the scheduled method would execute writes to DB and to the IO File system to delete the db entries and the physical files, it is vital to me that the method executes completely or does not execute at all, to keep the db consistent with the files stored at hd.

我的第一个选择是FluentScheduler,但我需要知道使用这样的框架是否是最佳解决方案.

My first bet is for FluentScheduler, but i need to know if using frameworks like that is the best solution.

我知道在StackOverflow中不欢迎此类问题.这不是一个意见问题,我只想阅读解决方案,然后选择最适合我要求的解决方案即可.

I know that this kind of questions are not welcome in StackOverflow. It is not an opinion question, i just want to read the solutions and select the one that best fits my requirements.

这是调度方法内部的某种伪代码:

Here is some kind of pseudo-code of what would be inside the scheduled method:

using(ViewMediaDBUnitOfWork uw = new ViewMediaDBUnitOfWork())
        {
            var today = DateTime.Now.Date;

            List<Media> mediaToDelete = uw.MediaRepository.Get(m => (m.ToDelete - today).value.Days > 30);
            mediaToDelete.ForEach(m =>
            {
                try
                {
                    //Deletes from DB
                    uw.MediaRepository.Delete(m);
                    //Deletes the file
                    File.Delete(m.Path);
                    //If everything is ok, savechanges
                    uw.MediaRepository.SaveChanges();
                }
                catch(Exception e)
                {
                    LogManager.GetCurrentClassLogger().Error("Error deleting. " + m.Path, e);
                }                    
            });
        }

推荐答案

如果要安排任务,至少要至少在

If you are going to schedule a task, at a minimum you’d want to at least do it within the context of HostingEnvironment.QueueBackgroundWorkItem. This will register your work with the ASP.NET runtime. Though there are caveats. When ASP.NET recycles for whatever reason, it will notify the background work (by setting a CancellationToken) and it will then wait up to a specified period of time (I think 30 seconds) for the work to complete. If the background work doesn’t complete in that time frame, the work will be gone.

FluentScheduler和Quartz是很好的框架,但是如果可以避免,则应避免在ASP.NET应用程序的上下文中调度工作(由于上述原因).您可以创建一个使用这些框架在应用程序外部安排作业/重复任务的服务.

FluentScheduler and Quartz are good frameworks, but you should avoid scheduling work within the context of your ASP.NET application if you can avoid it (for the reasons stated above). You could create a service that that uses these frameworks to schedule jobs / recurring tasks outside of your application.

但是,更可靠的选择是使用类似 Hangfire 的技术/框架,该技术/框架可与可靠的存储,例如SQL Server,Redis或MSMQ.

However, a more robust choice is to use a technology / framework like Hangfire which works in conjunction with some kind of reliable storage such as SQL Server, Redis, or MSMQ.

Hangfire是一个开源框架,可帮助您创建,处理并管理您的后台工作,即您不想进行的操作在您的请求处理管道中.

这篇关于在C#.NET Web应用程序中每天安排任务的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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