Proper way to schedule a task daily in C# .NET web application

后端 未结 2 466
半阙折子戏
半阙折子戏 2021-01-18 15:39

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

相关标签:
2条回答
  • 2021-01-18 16:14

    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 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.

    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 is an open-source framework that helps you to create, process and manage your background jobs, i.e. operations you don't want to put in your request processing pipeline.

    0 讨论(0)
  • 2021-01-18 16:36

    It sounds like you would be better off creating a separate service to handle the deletion of the timestamped records rather than trying to bundle it in to your existing application.

    As one very quick solution, It would take about 10 minutes to create a Powershell script that would run an SQL query, deleting data where the TimeStamp > x date.

    You could then schedule this script to run every day. It would be much more reliable than hoping a IIS worker pool thread would still be running.

    There are a few other ways also, such as SSIS but that might be more complicated than what you need.

    0 讨论(0)
提交回复
热议问题