a blog for those who code

Saturday, 2 July 2016

Schedule Tasks in Asp.Net Web Application

In this post we will be discussing about scheduling tasks in Asp.Net Web Application. Scheduling regular tasks can be anything for example sending emails once every day, tweeting about anything etc. We will be using Quartz.Net which is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems.

This article is very basic implementation that will get you up and running with a schedules email job. The basic features of Quartz.Net includes Job Scheduling, Job Execution, Job Persistence and Clustering.

At first we are going to install Quartz from Nuget. Right Click on Solution of any project in which you want to include Quartz.Net and click on Manage Nuget Packages.


After that search for Quartz.Net and Install it. After installing the reference will be added in your project.


For scheduling a Job you need to know about 3 components, job, trigger and scheduler. A job is the task which needs to be performed (sending email, tweeting). Trigger is about scheduling that is how and when the job to be executed. Scheduler is nothing but a agent which takes care that job is performed on the schedule dictated by the trigger.

At first we will create a Job Class, which should implement IJob interface and which should have one Execute method as shown below :

using System;
using Quartz;
using Quartz.Impl;

namespace SchedulingTasks
{
  public class MyJob : IJob
  {
    public void Execute(IJobExecutionContext context)
    {
       // Send Email Or Tweeting
    }   
  }
}

In the above example we have a Class MyJob which has implemented IJob interface and have a method Execute taking IJobExecutionContext object as parameter. Context contains the configuration data about the job. Now inside the the execute method you can do anything which you want to schedule for example sending emails or tweeting.

Now the next thing is to set up a Trigger which will trigger the execute method of the Job Class.

using System;
using Quartz;
using Quartz.Impl;

namespace SchedulingTasks
{
  public class JobScheduler
  {
    public static void Start()
    {
      IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
      scheduler.Start();

      IJobDetail job = JobBuilder.Create<MyJob>().Build();

      ITrigger trigger = TriggerBuilder.Create()
            .WithSchedule(CronScheduleBuilder
            .WeeklyOnDayAndHourAndMinute(DayOfWeek.Tuesday, 11, 0)
            .InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("UTC")))
            .Build();

      scheduler.ScheduleJob(job, trigger);
    }
  }
}

In the above code we have a Class JobSchduler and a static method Start. In this method we will create a scheduler and we will start it by calling scheduler.Start(). Then we will create a job using Quartz.Net and pass our MyJob Instance and build it.

Next is to create a trigger which will run the above job at specific time of the day or could be anything. We will create a trigger using TriggerBuilder. The trigger which we have created will send the email every Tuesday at 11:00 AM UTC Time Zone. To know more about TimeZoneInfo.FindSystemTimeZonebyID you can refer How to get Timezone ID's in C#.

Another example of a trigger is shown below which will trigger the job once every 100 Minutes.

ITrigger trigger = TriggerBuilder.Create()
 .WithSimpleSchedule(a=>a.WithIntervalInMinutes(100).RepeatForever()).Build();

Now both the job and trigger has been created and both of them have been registered with our scheduler. Now its time to start our scheduler whenever our application start. So the best place to start the scheduler is to write in global.asax's Application_Start() method.

public class Global : HttpApplication
{
  void Application_Start(object sender, EventArgs e)
  {
    JobSchduler.Start();
  }
}

Like this you can schedule your tasks in your Asp.Net Web Application. Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.


2 comments:

  1. Look into Hangfire (http://hangfire.io/) for an alternative to Quartz.

    ReplyDelete
  2. Shameless plug: Chroniton was built specifically and an alternative to Quartz. Quartz is robust, but it does have some weight that comes with it, specifically, other dependencies and stingly typed access to your data in the context of the Execute method. There are better and lighter-weighted options. https://github.com/leosperry/Chroniton

    ReplyDelete

Contact Us

Name

Email *

Message *

Sponsers

Best Hosting Plans

CC License