Azure Functions

Azure Functions are one of those technologies you don’t know you want, until you use it. They allow you to run C# or JavaScript, based on a trigger or event. It has no UI, just a log output, and is ideal for running reoccurring jobs, but didn’t want to setup a server to do it. That is simply it, a place to run a small piece of code, network enabled, configure the triggers and not have to worry about any setup or maintenance of servers. This technology is similar to AWS Lambda and Google Cloud Functions.

Create Azure Function

In the Azure Portal, search for functions.

Fill out the required information. Regarding the plan, this is up to you, however the consumption plan, as of this post date, gives you 1 million free executions per month. For testing purposes, its great, as you will unlikely ever hit that mark. Confirm with Azure Function Pricing to see all the details. Please note that Azure Functions requires a storage account, which is not included in the free grant.

Getting Started

Once your press create and the Azure Function is deployed, this may take a few moments, you will see the resource below.

If you are opening for the first time, it will show you a quick start screen. For this example, I will be using a C# function, with a timer trigger.

Develop

On the left hand side, if you select, Develop, then on the right hand side, select View Files and the run.csx, you will see the C# code that is to be run on each timer tick. The code runs on the full .NET Framework, however it also supports .NET Standard 1.3 packages.

Press Run, if you want to manually run your code and see the result in the output window.

Nuget Packages

You can even use Nuget Packages with your code. First, you go to the right hand side and add a new file, call it project.json. Add in the following text, and change the dependencies to what your code needs. As you will see in the logs, once it is saved, it will download and restore the packages.

Here is the code for copy and pasting if needed.

{
 "frameworks": {
     "net46":{
         "dependencies": {
             "Newtonsoft.Json": "9.0.1"
         }
     }
 }
}

Integrate

Integrate is setting the triggers that will call your Azure Function. In this example, a timer that runs every 5 minutes. The Schedule, is set in a cron expression. If you need to see how to write one, expand the documentation link on that page and it will detail how to. Timers are just one way to trigger a job, you can also do so by event hubs or web calls.

Monitor

To monitor how your function is doing, go to the monitor tab on the left hand side. Here you have a record of when your code run and if it was successful.

Summary

Azure Functions are great for small easy workloads, instead of running a service on a server, or building an entire API. Using them as Webhooks or processing data in the back-end of your API for your mobile app, the possibilities are vast for this seemingly innocent little feature on Azure.

Microsoft MVP | Xamarin MVP | Xamarin Forms Developer | Melbourne, Australia

Related Posts

Leave A Comment?