Job Scheduling in ASP.NET MVC website using Quartz scheduler

In this article, I will show you how to schedule a job in ASP.NET MVC using quartz scheduler library.

Introduction

Every ASP.Net MVC Developers know that how to create ASP.Net MVC web applications that run in the background but most of the time such background tasks are handled by creating windows service with timer or console based application. we are going to discuss one job scheduling in ASP.Net MVC program by using quartz DLL libraries.

Now you have an asp.net MVC web based application where you want to perform job scheduling(run some tasks in a background) by quartz job scheduler as scheduled interval. now, we can call any asp.net method using AJAX on timely based interval basis. but what happens if there is no traffic on your asp.net website. here we have one solution for job scheduling through we can create a background job scheduling which keeps on running even if there are not any live website visitor. so that finally we have to create a web form which can run continuously in a background in asp.net MVC web application which constantly runs in the background using ASP.NET MVC web form?

Problems with Job Scheduling in ASP.Net MVC Website

ASP.NET web form doesn’t provide such controls or methods which able to call method once per defined time interval like call method after every 2 hours later, call a method after every 1 day later or call a method after every 1 month later etc.

so here I am going to show you how to run background job in ASP.Net as well I will show you what are the configuration settings which needs to be done to keep web application running even after 20 minutes of default ideal time.

Solutions

There is a DLL ( dynamic link library) which provide this kind of functionality. It is called quartz.net scheduler.
This is simple to understand and easy to implement, Please download the DLL or NuGet package for the  quartz.net and everything is there for a project to get started.

You can install this package on NuGet package console by typing following command:
Install-Package Quartz

Here is the job scheduler solution in asp.net c# sample code is as follow.

Create Class for job schedule which inherit IJob class of quartz.net.

Hire Now!

Do You Need Help From Dedicated ASP.NET Developer for Hire?

Hire our best ASP.Net developer on hourly, Full-time, Part-time bases. We are ready to help you resolve the problems for ASP Net website development.
Hire Now!

Example :

public class ScheduleJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Email ObjEmail = new Email(); ;
            ObjEmail.SendEmails();
        }
    }
	
 Then create other executable method where you can job schedule for your bunch of tasks

Example :

public static void Start()
 {
 IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
 scheduler.Start();
 
IJobDetail job = JobBuilder.Create<ScheduleJob>().Build();
 
 ITrigger trigger = TriggerBuilder.Create()
 .WithIdentity("trigger1", "group1")
 .StartNow()
 .WithSimpleSchedule(x => x
 .WithIntervalInMinutes(1)
 .RepeatForever())
 .Build();
 
 scheduler.ScheduleJob(job, trigger);
 }

This trigger will run every minute

Note : There are more way to create trigger you can check the site for more details.

Then call the schedule in Application start
Example :
JobScheduler.Start();

This will start job schedule for the event which run in the background on every minute.

Now there is one additional settings in IIS, this will keep your IIS application pool to keep alive until your web server is restarted.

  1. Open the web server IIS
  2. Go to the ApplicationPool
  3. Choose your App pool
  4. Open Recycling
  5. Set Regular Time Interval to 0 (Zero)

This settings will not recycle your App pool even if it is not visible to the world for long period of time.

I hope this all information will works for you.
By | 2017-09-20T10:52:05+00:00 October 3rd, 2016|Asp.Net, Asp.Net MVC, E-Commerce|