Create Azure Timer Function Using C#

Please go through the article given below to understand Azure functions.

In this article, we will create our first Timer function, using C#.

Navigate to the Quick Start page, as shown below. You can navigate to the Quick Start page by clicking Azure function app in the app Service listing.


We will just create a simple Azure function, which prints a message every 5 seconds. For this example, I have chosen C# as the language to develop the Azure function. You can either choose C# or JavaScript.

Once you choose Azure function template and the language in which you would like to develop the function, click Create this function button, as shown above.

You can also click Create your own custom function, if you prefer any language other than C# and JavaScript.

As of this writing, the languages are given below, which are currently supported to develop Azure functions.

Clicking Create this function button will create a new Azure function and takes you to the page given below.


By default, a file's name is run.csx (.csx is the extension of the Azure Function files) and will be created with the code given below.

  1. using System;  
  2. public static void Run(TimerInfo myTimer, TraceWriter log) {  
  3.     log.Info($ "C# Timer trigger function executed at: {DateTime.Now}");  
  4. }  

This is the simplest function, which you can create, using Azure functions. Basically, this function would insert a log entry with a simple message along with the time.

As shown below, click Run button to execute the function.


As soon as you click Run button, the program will execute and prints the output in the log Window, as shown below.

  • 2017-03-05T17:21:33.185 Function started (Id=ef0e3ae4-8653-496d-b466-b1c7170f6dbd)
  • 2017-03-05T17:21:33.185 C# Timer trigger function executed at: 3/5/2017 5:21:33 PM
  • 2017-03-05T17:21:33.185 Function completed (Success, Id=ef0e3ae4-8653-496d-b466-b1c7170f6

By default, the Timer function runs after every 5 minutes. You can change the frequency of the time by navigating to the Integrate tab and changing the value in the Cron expression of the schedule input box, as shown below.


Once you change the Cron expression, click Save button, as shown above.

Summary

We have learned the following.

  • Create the Time function, using C#.
  • Change the time frequency in Cron expression.
  • View the logs of Azure function.