Hi @mc,
You can use Hangfire to complete, you need to configure the corresponding cron expressions to set the time.
For example,
Firstly, you need to install the corresponding Nuget Packages: Hangfire, Microsoft.Data.SqlClient:
Then configure Hangfire in Program.cs:
//...
builder.Services.AddHangfire(x => x.UseSqlServerStorage("Server=(localdb)\\mssqllocaldb;Database=ProCodeGuide.Samples.Hangfire;Trusted_Connection=True;MultipleActiveResultSets=true"));
builder.Services.AddHangfireServer();
//...
app.UseHangfireDashboard();
app.UseHangfireServer();
//add your job, you can also add this in other class, for example: Controller
RecurringJob.AddOrUpdate(() => Console.WriteLine("This is my test job"), "55 13 * * *", TimeZoneInfo.Local);
//...
You need to create your connection database and the table structure will be added automatically when running.
Note: If you are implementing a non-static method, please refer to the way in this GitHub link.
Test Result:
You can access the dashboard by typing /hangfire
after your url:
Of course, you can also choose other ways to complete:
- Native .NET Core solution through IHostedService.
- Quartz.NET.
- Azure Queue Storage, and here is a case about this: schedule a background task (Cron Jobs) with hosted services for a specific date and time in the future.
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Chen Li