Monday, May 2, 2016

Running Web Job on Schedule


To get a Web Job, start by making a Web App. The Web Job is associated with the Web App. You can either actually use the Web App, or just make an empty one as a placeholder for your Web Job. Web Jobs can be used for: continuously running a background task at a certain time interval in the cloud. In order to run a web job on a schedule, you have to include the Web Job NuGet package. You can either setup a normal console application, or setup a "web jobs" console application (which has the Web Job references already added for you along with the JobHostConfiguration code below). You can add the [TimerTrigger] attribute as a parameter into the main function you would like to use. Make sure to put the JobHostConfiguration into the Main method, if you do not then your web job will likely continuously loop and not work.

using System;
using System.Net;
using Microsoft.Azure.WebJobs;

namespace Stuff
{
    public class Program
    {
        public static void Main(string[] args)
        {
            JobHostConfiguration config = new JobHostConfiguration();
            config.UseTimers();

            JobHost host = new JobHost(config);
            host.RunAndBlock();
        }

        public static void ProcessDataTransfer([TimerTrigger("01:00:00", RunOnStartup = true)] TimerInfo timerInfo)
        {
            // Your Starting Method Code Here
        }
    }
}