Azure Functions

What are Azure Functions?

Azure Functions are small scripts that will be triggered on a action like adding files onto a blob storage, or receiving messages on a service bus. After triggering, it execute some logic and either send it back or to another location. So you can call it a “nano service” most commonly coded in c# or node.js . Be sure that a function will ever be triggered by something, it does not run by it self (exept a timer job).

How can I produce some code?

This is simple, you can use the coding editor in the azure Portal, there you have the possiblility to execute and test the functions. As alternative you can use VS Code in combination with scriptcs to produce some Azure Functions.

What’s about the code?

The content of a function in c#are like following:

At the first look it seems that it is usual C# code. But this is not a .cs file that will be compiled to a binary. thsi give us the possibility to change the logic on runtime without recompiling the whole application.

What about my references like in C# native code?

You see in the example above a keyword called “#r”. This give you the possiblity to reference extra assemblies.

By default there will be referenced the following assemblies by the runtime itself:

  • mscorlib
  • System
  • System.Core
  • System.Xml
  • System.Net.Http
  • Microsoft.Azure.WebJobs
  • Microsoft.Azure.WebJobs.Host
  • Microsoft.Azure.WebJobs.Extensions
  • System.Web.Http
  • System.Net.Http.Formatting

Default additional assemblies (that will be loaded with “#r” statement like above), are:

  • Newtonsoft.Json
  • Microsoft.WindowsAzure.Storage
  • Microsoft.ServiceBus
  • Microsoft.AspNet.WebHooks.Receivers
  • Microsoft.AspNEt.WebHooks.Common

When you want to load a self coded custom super duper assembly, you must do the following steps:

  1. Deploy your assembly into a “bin” (create if is not there) in you function folder.
  2. Reference the Assembly in the code wirh “#r “MyCustomAssembly.dll

That’s all, you can now your custom code that will be available within your assembly. Please make sure, that you don’t forget the “.dll”!.

What about nuget?

NuGet is the package manager for the Microsoft development platform including .NET. The NuGet client tools provide the ability to produce and consume packages. The NuGet Gallery is the central package repository used by all package authors and consumers.” (source: https://www.nuget.org/).

So is it possible to add nuget packages for refence them?

Yes it’s possible. For this you must add file called “project.json” into your functionapp folder. In this json file you must put in the references you want to use. Here’s a example:

This example project.json adds the Pakage “Microsoft.ProjectOxford.Face”. When you upload this file you will see in the output log that the runtime download the package and installing it.:

You register that the package was now installed by the engine. From now you can use the assembly. The main difference in using this method is, that you must not reference the package with an “#r” keyword. You must only set the correct using.

Conculsion

I think that Azure Functions show it’s strength in Enterprise applications, especially when you want to make a connection between systems when they can’t connect together. So you can now build a middleware that scales autmatically.