Create Azure Timer Function using C#

Please go through the following article to understand about Azure Functions.

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

Navigate to the Quick Start Page as shown in the below screen capture, you can navigate to the Quick Start page by clicking on the Azure Function App in the App Service listing.

1_quickstart

We will just create a simple Azure Function that prints a message every 5 seconds. For this example, I have chosen C# as the language for developing the Azure Function. You can choose either C# or JavaScript.

Once you chose the Azure Function template and the language in which you would like to develop the function, click on the Create this function button as shown in the above screen capture.

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

As of this writing below are the languages that are currently supported for developing Azure Functions.

2_languages

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

3_functioneditor

By default, a file names run.csx (.csx is the extension of the Azure Function files) will be created with the following code.

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

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

As shown below, click on the Run button to execute the Function.

4_runbutton

As soon as you click on 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 every 5 minutes. You can change the frequency of the time by navigating to the Integrate tab and change the value in the Cron expression of the Schedule input box as shown below.

5_navigatetab

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

Summary: we have learnt the following.

  • Create the Time Function using C#
  • Change the time frequency in the Cron expression
  • View the Logs of the Azure Function

5 comments

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 )

Facebook photo

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

Connecting to %s