Hangfire is a job scheduling framework that provides background processing without needing any windows service. That's the main point and competitive advantage of Hangfire in comparison to other similar libraries. Imagine sending a million messages or email in your application, you don't need to force the user to wait for all the processes to be completed. Despite, there are lots of ways to accomplish the mentioned job; Hangfire uses the request processing pipeline of ASP.Net for processing and executing jobs however it runs in every framework of Microsoft!
In order to create delayed jobs, just call the BackgroundJob.Schedule(). Create a console application and install the library from nuget and call Schedule(,) :
BackgroundJob.Schedule( () => Console.WriteLine("Job Scheduled!"), TimeSpan.FromDays(5));
The above code simply will be scheduled for the job from 5 days later. You can also run the task on a specific date like this:
BackgroundJob.Schedule( () => Console.WriteLine("Job Scheduled!"), new DateTime(2017, 02, 05, 12, 00, 00));
Not that the default time of enqueueing the scheduled jobs by hangfire server is 15 seconds which is changeable. To change that default periodical schedule, initiate the BackgroundJobServerOptions in the constructor:
var options = new BackgroundJobServerOptions { SchedulePollingInterval = TimeSpan.FromMinutes(5) };
HangFire Server is responsible for background job processing. To start the background processing, create an instance of BackgroundJobServer and start it.
var server = new BackgroundJobServer(options); server.Start();
At the time of writing this post, the start() of BackgroundJobServer() is obsolete! You don't have to call start, it will be started automatically by the constructor. Call dispose() to waiting for server shutdown:
server.Dispose();