If I have a recurring job that will be run daily inside the .net core application. For that purpose, I'm thinking to use the Hangfire library.
My question is: will placing this code for executing this daily job here in the Configure method be the most logical place or you would suggest something else?
public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, IHostingEnvironment env)
{
app.UseHangfireDashboard();
backgroundJobs.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));
...
}
As your code is written it won't trigger daily, unless you restart your application on a daily basis. Also, if you you have multiple instances of your application, you will get multiple runs of your background job.
What you need to write should be something like :
RecurringJob.AddOrUpdate( "MyConsoleJobUniqueId",
() => Console.WriteLine("Hello world from Hangfire!"),
Cron.Daily );
As said by @BrandoZhang this code would be better placed in appLifetime.ApplicationStarted
event handler.