ASP.NETCore2.1中的Cronjob

ASP.NETCore2.1中的Cronjob,asp.net,asp.net-web-api,cron,Asp.net,Asp.net Web Api,Cron,我对克朗·乔布斯有意见。我尝试了很多代码,但都不管用。我真的不明白原因。这是我的密码 public class CronJobServices : BackgroundService { private CrontabSchedule _schedule; private DateTime _nextRun; private string Schedule => "0/20 * * ? * * *"; //Runs every 20 seconds pu

我对克朗·乔布斯有意见。我尝试了很多代码,但都不管用。我真的不明白原因。这是我的密码

public class CronJobServices : BackgroundService
{
    private CrontabSchedule _schedule;
    private DateTime _nextRun;

    private string Schedule => "0/20 * * ? * * *"; //Runs every 20 seconds

    public CronJobServices()
    {
        _schedule = CrontabSchedule.Parse(Schedule, new CrontabSchedule.ParseOptions { IncludingSeconds = true });
        _nextRun = _schedule.GetNextOccurrence(DateTime.Now);
    }


    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        do
        {
            var now = DateTime.Now;
            var nextrun = _schedule.GetNextOccurrence(now);
            if (now > _nextRun)
            {
                Process();
                _nextRun = _schedule.GetNextOccurrence(DateTime.Now);
            }
            await Task.Delay(5000, stoppingToken); //5 seconds delay
        }
        while (!stoppingToken.IsCancellationRequested);
    }

    private void Process()
    {
        InfoServer.SendMail("mymail@gmail.com", "Hello", "CronJobServices");
    }
}
在Startup.cs中

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddSingleton<CronJobServices>();
    }
public void配置服务(IServiceCollection服务)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton();
}
InfoServer.SendMail是一种简单的发送邮件的方法,它工作得很好,我出于其他原因使用了很多

我甚至试着使用IHostedService,但都没用。 有什么想法吗

编辑:CrontabSchedule从nContab NUGet中获取