Creating a Job Scheduler for .NET application
Sometimes we need to create Job Scheduler for web applications. For an example we might need to send automated emails to end users based on some conditions. This task is bit challenging but don’t worry. We have some fully featured Nuget packages we can use for this task. In this article we are building a Job Scheduler for .NET applications with Quartz.Net.
I’m going to use Quartz.Net Enterprise Job Scheduler for .NET.NET package for this tutorial and show you how to create a Job Scheduler for web applications. The version I’m using for this is Quartz 2.6.2. I have created a MVC web application for this tutorial. So we are going to implement a job scheduler for our web application.
First go to nuget package manager and install the package Quartz (by Lahma).
https://www.nuget.org/packages/Quartz/
Then We need to create the TaskScheduler class.
public static class TaskSchedular
{
public static void Start()
{
try
{
// Grab the Scheduler instance from the Factory
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
// and start it off
scheduler.Start();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<EmailReminder>()
.WithIdentity("EmailReminder", "Reminders")
.Build();
// Trigger the job to run, and then repeat every 24 hours
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "Reminders")
.StartAt(new DateTimeOffset(2019,06,19,00,30,00,DateTimeOffset.Now.Offset)) // Start at 2019/06/19 : 12.30 AM and run every 24 hours
.WithSimpleSchedule(x => x
.WithIntervalInHours(24)
.RepeatForever())
.Build();
// Tell quartz to schedule the job using our trigger
scheduler.ScheduleJob(job, trigger);
// and last shut down the scheduler when you are ready to close your program
//scheduler.Shutdown();
}
catch (SchedulerException se)
{
// Error logging
}
}
}
StartAt(new DateTimeOffset(2019,06,19,00,30,00,DateTimeOffset.Now.Offset))
This will make the task to start on 2019/06/19 12.30 AM and run every 24 Hrs. Rather than hard coding the values we can use web config to keep this values if we want to change the values in future.
Then create the job class implementing the IJob interface from Quartz. This class is the job class and we can implement our email sending logic here.
public class EmailReminder: IJob
{
public void Execute(IJobExecutionContext context)
{
try
{
// Implement your business logic to send emails
}
catch (Exception ex)
{
// Error logging
}
}
Then register the TaskScheduler in Global.asax file as below. By registering this inside Global.asax file we are setting our job scheduler to start when the application starts. But still the task will start at the time we set in Job class.
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
UnityConfig.RegisterComponents();
TaskSchedular.Start();
}
For testing, we can set the running period by changing WithIntervalInHours method. We can use WithIntervalInMinutes method instead and pass 1 or 2 minutes. Then task will run every 1 or 2 minutes.
If you come across any issues or errors, comment below and I may take a look at it and help you. Happy coding!
i have some errors, can you post your source code for check, please.
Greetings
Hi Adrian, Full code is there. Can you post your errors please ?
Hello
I try using your example but in asp.net web application, (no MVC), and i have 6 errors:
Error 1 The name ‘LogWriter’ does not exist in the current context
Error 2 The name ‘Common’ does not exist in the current context ( and i using System.Web.Configuration.Common;)
Error 3 The type or namespace name ‘Common’ could not be found (are you missing a using directive or an assembly reference?)
Error 4 The name ‘Common’ does not exist in the current context
Error 5 Cannot implicitly convert type
‘System.Threading.Tasks.Task’ to ‘Quartz.IScheduler’. An explicit conversion exists (are you missing a cast?)
Error 6 The name ‘LogWriter’ does not exist in the current context.
I have Quartz.dll imported in my project, but when i build the project show these errors..
Thanks for Help.. Greetings.
And the most important error (sometimes when build the project, don´t show the 6 errors aboved but show these:)
Error 1 ‘EmailReminder’ does not implement interface member ‘Quartz.IJob.Execute(Quartz.IJobExecutionContext)’. ‘EmailReminder.Execute(Quartz.IJobExecutionContext)’ cannot implement ‘Quartz.IJob.Execute(Quartz.IJobExecutionContext)’ because it does not have the matching return type of ‘System.Threading.Tasks.Task’.
Hi Adrian,
Those are for logging. I have removed logging related codes.