Job Scheduling In ASP.NET MVC With Quartz.NET

Introduction

Job scheduling in ASP.NET MVC with Quartz.NET is an open source job scheduling framework written in C# for .NET. job scheduling of background tasks that can be used for any application system. Sometimes everyone gets some issue like bulk email sending one by one that takes lot of time. W can use this job scheduling;  it works on background tasks in our Application (MVC or ASP.NET websites)
 
Figure 1 :
 
First create a application and install Quartz.NET in our application is via Nuget ,
 


We can do this by typing Install-Package Quartz at the Package Manager Console prompt or by right clicking on the project in Visual Studio Solution Explorer and select Manage Nuget Packages.
 
Figure 2 :
 
Search "quartz" and click Install button,
 
 
 
Three major categories are :
  1. Defining Job
  2. Job Trigger
  3. Job Schedule
Job is the task, Job Trigger is when the job is executed. Job and trigger are combined for execution with the JobScheduler . We have to create a job class which implements the IJob interface defined by quartz.net. This class is called by the quartz.net scheduler at the configured time and should therefore contain your send mail functionality 
 
Defining Job

A job is a class. It implements the Quartz IJob interface  and this method defines actions to be performed. The Execute method get IJobExecutionContext object as a parameter. The scheduler passes that in when calling the job's Execute method. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using Quartz;  
  6. using System.Net;  
  7. using System.Net.Mail;  
  8.   
  9.   
  10. namespace ScheduledTask.Models  
  11. {  
  12.     public class Jobclass:IJob  
  13.     {       
  14.         public void Execute(IJobExecutionContext context)  
  15.         {  
  16.             using (var message = new MailMessage("[email protected]""[email protected]"))  
  17.             {  
  18.                 message.Subject = "Message Subject test";  
  19.                 message.Body = "Message body test at " + DateTime.Now;  
  20.                 using (SmtpClient client = new SmtpClient  
  21.                 {  
  22.                     EnableSsl = true,  
  23.                     Host = "smtp.gmail.com",  
  24.                     Port = 587,  
  25.                     Credentials = new NetworkCredential("[email protected]""123546")  
  26.                 })  
  27.                 {  
  28.                     client.Send(message);  
  29.                 }  
  30.             }  
  31.         }  
  32.     }  
  33.   
  34. }  
Job Trigger

Actual code has been placed in a method called Start A scheduler is created and started the job is created using the Quartz.NET JobBuilder.Create<Jobclass> method, where Jobclass is the type of job to be created. Then a trigger is created when a Job should be executed. 
 
We want to run this Job WithIntervalInSeconds once in Seconds . So our trigger definition will look like, 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using Quartz;  
  6. using Quartz.Impl;  
  7.   
  8. namespace ScheduledTask.Models  
  9. {  
  10.     public class JobScheduler  
  11.     {  
  12.         public static void Start()  
  13.         {  
  14.             IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();  
  15.             scheduler.Start();  
  16.   
  17.             IJobDetail job = JobBuilder.Create<Jobclass>().Build();  
  18.   
  19.             ITrigger trigger = TriggerBuilder.Create()  
  20.             .WithIdentity("trigger1""group1")  
  21.             .StartNow()  
  22.             .WithSimpleSchedule(x => x  
  23.             .WithIntervalInSeconds(10)  
  24.             .RepeatForever())  
  25.             .Build();  
  26.   
  27.             scheduler.ScheduleJob(job, trigger);  
  28.         }  
  29.     }  
  30. }  
There are different types of triggers – Time based, Calendar, Schedule, etc.
 
JobScheduler in Global asax

The Application_Start event handler in the Global.asax.cs file place for invoke the start method of the JobScheduler.Start, as it would be called the first time the application is started.
 
Summary:
 
We learned open source job Scheduling Background Tasks in ASP.NET using Quartz.NET. I hope this article is useful for all .NET beginners. 
 
Read more articles on ASP.NET: 


Similar Articles