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

Advertisement

One thought on “How to Schedule Task In ASP.NET using Quartz.Net ?

  1. Quan Nguyen March 20, 2017 / 11:10 am

    When I publish the project on IIS, but it is not working with expect. Please help me!!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.