简体   繁体   English

如何调度数据库任务 ASP.NET Core?

[英]How to schedule a database task ASP.NET Core?

I've built a simple student management system.我已经建立了一个简单的学生管理系统。 In my application, I need a scheduler for 1st day of every month student due amount will increase by the given number and update student info in the database.在我的应用程序中,我需要一个调度程序,用于每个月的第一天学生到期金额将增加给定数量并更新数据库中的学生信息。

I tried with hangfire and it worked well on the local machine, but it did not work when I deployed the application to the web server.我尝试使用hangfire,它在本地机器上运行良好,但是当我将应用程序部署到web 服务器时它不起作用。

Now how can I do these kinds of mechanisms?现在我该怎么做这些机制?

Thank you.谢谢你。

You can use the IHostedService interface and register using:您可以使用 IHostedService 接口并使用以下方式注册:

services.AddHostedService<MyScheduler>();

MyScheduler我的调度器

public class MyScheduler : IHostedService
{
    public async Task StartAsync(CancellationToken cancellationToken)
    {
        // Start a timer that will run every x minutes to see
        // if the conditions are met to perform your action
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        // Dispose of Timer
    }
}

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio

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

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