A blog by Geert van der Cruijsen on Apps, Cloud & ALM

Azure functions are great to build small specialized services really fast. When you create an Azure Functions project by using the built-in template from the SDK in Visual Studio you’ll automatically get a function made in a CSX file. This looks like plain old C# but in fact it is actually is C# Script. When you’re deploying these files to Azure you don’t have to compile them locally or on a build server but you can just upload them to your Azure Storage directly.

In the last update for Azure Functions the option to build precompiled functions was added. Doing this is actually pretty simple. I’ve created a sample project on Github containing a precompiled Azure function, unit tests for the function and an ARM template to deploy the function. Lets go over the steps to create a precompiled Azure function.

Build

In this example we will just create a super simple Azure function. We’ll borrow the code from the CSX Function template and turn that into a precompiled Azure function. Instead of creating a project from the Azure Function template we can just use a new Class library and call this “PreCompiledFunctionDemo”.

In the Class library project we’ll delete the generated Class1.cs and create a new Class containing our function. Let’s name the class: “NameFunction” In this class we can just copy and paste the code from our CSX example function in this instance. If you are creating your own function make sure you create a public class containing a static method called Run.

To get the class library to build we have to add a couple of Nuget Packages: Newtonsoft.Json, Microsoft.AspNet.WebApi.Core and Microsoft.AspNet.WebApi.Client. Now the Function compiles and we can add the last step to get a working function: Adding the Function.json file. The function.json file is works just the same as with CSX Functions except you’ll have to add a couple of lines at the top that point towards your Assembly containing the function. Add these 2 lines containing the ScriptFile and the EntryPoint and now our function is ready!

Test

One of the nice things of precompiled functions is that you can use tools for unit testing your function as you would when building other .Net code. In this example i used XUnit because that seems to be the most popular testing framework nowadays. Since my function is just a plain old class library it is easy to add some tests.

In our unit test we can just call the static Run method on our NameFunction class and pass in a HttpRequestMessage. The URL doesn’t matter since it’s no actually hosted although you can add URL parameters to your function using the querystring like i do in the first test.

The HttpRequestMessage also needs to get an HttpConfiguration passed in otherwise your function will not be able to create a response on the HttpRequestMessage. After these arrangements are done we can call the Run method and assert our outcome.

Deploy

So now our Class library can be built and tested but how do we get this DLL to Azure? The simplest way you can do this is going to the Azure portal into the development area of your Azure function and drag the DLL + function.json file into the file list on the right.

This might be the easiest way to do it once. my guess is that if you want to build a precompiled function you’re going to set up CI / CD for your function. In this example i’ll set it up using VSTS. To do this we’re going to borrow an ARM template my Xpirit colleague Peter Groenewegen created that can be used to create all the Azure components we need for our Azure function. This ARM template will be used in our Release in VSTS to create a new function app where we will then deploy our DLL and function.json file on.

CI Build

I’ve hooked up my Visual Studio Team Services to my Github repo and set up a CI build so it will build on each pull request. I’ve selected the default Visual Studio build template and fill in all the build steps.

Select your solution file to restore nuget packages

Select your solution file to build the solution

Select your tests project assemblies so the build can execute the unit tests we created earlier.

Publish symbols

Copy the PreCompiledFunctionDemo.dll and the function.json file from the build output to a artifacts folder

Copy the json files from the ARM template to the artifacts folder

Publish the drop folder of the earlier copied artifacts

if we run this build we should get an all green results where our unit tests are ran and a package is created that we can then deploy to Azure.

Release

In the Release tab of Visual Studio Team Services we’re going to create a new release definition and we’re adding 2 steps.

2 Comments

Thanks for the excellent blog post. I’ve been trying to follow along and the only issue I’m running into is not knowing exactly what source/target and content should be set for the 2 copy steps. Could you possibly provide screenshots or clarifications as to what the settings should be for these build steps?

1 Pingback

About

This is a personal blog by Geert van der Cruijsen: Geert is a technical expert and architect on mobile and cloud technology based on the Microsoft stack. Geert has implemented enterprise mobility solutions and apps at several companies in the Netherlands and abroad. Geert is also an active co-organizer and speaker for several mobile developer communities.

Geert works as a Lead Consultant at Xpirit in the Netherlands. At Xpirit Geert helps Xpirit's customer with their Mobile strategy and vision from defining the right mobile moments to Xamarin expertise or setting up mobile development factory or mobile center of excellences. Geert is an expert on the full Mobile application development lifecycle.

Geert also works as a Xamarin Partner trainer doing class room Xamarin training for Xamarin University

This blog will focus on everything related to digital mobility from mobile app development tips using Xamarin, Android, iOS & Windows 10 to topics related to Microsoft Azure and ALM.