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 the Quartz scheduler library.

Introduction:

Every ASP.NET MVC developer knows 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 a timer or console-based application. we are going to discuss one job scheduling in the 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 the background) by quartz job scheduler as the scheduled interval.

Now, we can call any asp.net method using AJAX on a timely interval basis. but what happens if there is no traffic on your asp.net website? here we have one solution for job scheduling through which we can create a background job scheduling that keeps on running even if there are not any live website visitors.

So finally, we have to create a web form that can run continuously in the 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 Quartz Scheduler:

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

So here I am going to show you how to run a background job in ASP.NET as well I will also show you what are the configuration settings that need to be done to keep the web application running even after 20 minutes of the default ideal time.

Solutions:

There is a DLL ( dynamic link library) that provides 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 quartz.net and everything is there for a project to get started.

Note: You can install this package on the NuGet package console by typing the following command.

Install-Package Quartz:

Here is the job scheduler solution in ASP.NET C# sample code is as follows.

Create a Class for the job schedule that inherits the IJob class of quartz.net.

Example:


public class ScheduleJob: IJob
{
public void Execute(IJobExecutionContext context)
{
Email ObjEmail = new Email(); ;
ObjEmail.SendEmails();
}
}

Then create another 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 is 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 a job schedule for the event which runs in the background every minute.

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

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

These settings will not recycle your App pool even if it is not visible to the world for a long period.

I hope this all information will work for you.

Article by

Chintan Prajapati

Chintan Prajapati, a seasoned computer engineer with over 20 years in the software industry, is the Founder and CEO of Satva Solutions. His expertise lies in Accounting & ERP Integrations, RPA, and developing technology solutions around leading ERP and accounting software, focusing on using Responsible AI and ML in fintech solutions. Chintan holds a BE in Computer Engineering and is a Microsoft Certified Professional, Microsoft Certified Technology Specialist, Certified Azure Solution Developer, Certified Intuit Developer, and Xero Developer.Throughout his career, Chintan has significantly impacted the accounting industry by consulting and delivering integrations and automation solutions that have saved thousands of man-hours. He aims to provide readers with insightful, practical advice on leveraging technology for business efficiency.Outside of his professional work, Chintan enjoys trekking and bird-watching. Guided by the philosophy, "Deliver the highest value to clients". Chintan continues to drive innovation and excellence in digital transformation strategies from his base in Ahmedabad, India.