This is a ready made example of how to add a scheduled job to your ASP.NET core web application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class YourJob : IJob { public static async void Start() { IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); await scheduler.Start(); IJobDetail job = JobBuilder.Create<YourJob>().Build(); ITrigger trigger = TriggerBuilder.Create() .WithDailyTimeIntervalSchedule(s =>s.WithIntervalInSeconds(10)) .StartNow() .Build(); await scheduler.ScheduleJob(job, trigger); } Task IJob.Execute(IJobExecutionContext context) { return Task.Run(() => { Program.Counter++; }); } } |
In your Program.cs
1 2 3 4 5 6 7 8 9 |
public static int Counter = 0; public static void Main(string[] args) { var host = BuildWebHost(args); YourJob.Start(); host.Run(); } |