How to Schedule Task In ASP.NET using Quartz.Net ?


Hi

Before few years ago i wrote article on this topic like this

https://chandradev819.wordpress.com/2011/04/10/how-to-create-a-job-scheduler-in-asp-net-application/

But now the same task we can do in very efficiently using Quartz.Net open source library. It is very flexible to use in asp.net and asp.net mvc application.

To learn more about Quartz.NET here is a great article on its website.
quartz-scheduler.net

Step1 : Just add the libray in your application like this image

Quarz.net

Step 2: Create one one class in appcode like AutoSave and write the code which one you want to schedule

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for AutoSave
/// </summary>
public class AutoSave:IJob
{
	
    public void Execute(IJobExecutionContext context)
    {
       // This code is used to save data in database
        using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True"))
        {
            using (SqlCommand cmd = new SqlCommand("Insert into tblEmp(EmpName,EmpAdd) Values(@EmpName,@EmpAdd)",con))
            {
                cmd.Parameters.AddWithValue("@EmpName", "Chandradev");
                cmd.Parameters.AddWithValue("@EmpAdd", "Bangalore");
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }
}

Step 3: Now create on Jobscheduler class like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using Quartz.Impl;

/// <summary>
/// Summary description for JobSchedular
/// </summary>
public class JobSchedular
{

    public static void Start()
    {
        IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
        scheduler.Start();

        IJobDetail job = JobBuilder.Create<AutoSave>().Build();
        ITrigger trigger = TriggerBuilder.Create()
        .WithIdentity("trigger1", "group1")
         .StartNow()
         .WithSimpleSchedule(x => x
        .WithIntervalInSeconds(5)
        .RepeatForever())
    .Build();
        scheduler.ScheduleJob(job, trigger);
    }
}

Note here i have created a simple scheduler which will fire in every 5sec . You can create you schedular on basis of your requirement. If we have trigger only one time in whole day then we can do like this


  .WithDailyTimeIntervalSchedule
                  (s =>
                     s.WithIntervalInHours(24)
                    .OnEveryDay()
                    .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0))
                  )
                .Build();

Step 4: Add in global.asax file like this


void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterOpenAuth();
        JobSchedular.Start();
    }

I got the data in database like this.

Schedular

Posted in Asp.net. 1 Comment »