Using a Console Application in an Azure Web Job to Perform Scheduled Tasks

Recently I was tasked with developing a scheduled service which would be hosted in Azure. This service needed to retrieve data from a database and update the corresponding records in a Dynamics 365 instance. For this I decided to go with an Azure Web Job, over other options like Cloud Services and Azure Functions, mainly due to their simplicity in development and that they are very easy to deploy.

image thumb Using a Console Application in an Azure Web Job to Perform Scheduled Tasks

Azure Web Jobs can be two different types, continuous and triggered. Continuous web jobs are started as soon as the job has been created and are run in an endless loop. Triggered web jobs are only run when they are triggered manually or are running on a schedule. For this example, we will be using triggered jobs as we would set the job up to run on a schedule. The screenshot above shows a simple console application which would be used for the web job. When scheduled, this console application would run the Main method which would then run the logic you choose implement in the DoScheduledJobs method. For logging we can use Console.WriteLine which will print your logs in the Azure Portal for each of the jobs run. The screenshot below shows how the logs in portal would look.

image thumb 1 Using a Console Application in an Azure Web Job to Perform Scheduled Tasks

image thumb 2 Using a Console Application in an Azure Web Job to Perform Scheduled Tasks

The screenshot above shows the Azure Portal for a few existing web jobs I have previously created. Clicking the “Add” button brings the flyout on the right side of the screenshot and allows us to add a new web job. You would need to give the job a unique name, a zip file which contains the console application and a type of job. In this case I had selected the “Triggered” type with a “Scheduled” trigger and had set the schedule to be “0 5 * * * *”. This schedule uses a CRON expression with this example CRON expression scheduling the job to be run hourly on the 5th minute of the hour, for example 12:05, 1:05, 2:05, etc. Once you hit OK and select “Run” the schedule will begin and the console application will be triggered every hour. By clicking on “Logs” you will be able to see the jobs that were run and the logging you had added in the console application in real time.

Let’s block ads! (Why?)

Magnetism Solutions Dynamics CRM Blog