Building, testing and deploying precompiled Azure Functions
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.
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!
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.
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.
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.
In the Release tab of Visual Studio Team Services we’re going to create a new release definition and we’re adding 2 steps.
- Azure deploy step: select the ARM template azuredeploy.json file and parameters file to create a new Azure resource group.
- Web deploy step where we select the drop folder containing the dll file and the function.json file.
(Make sure the dll file and the function.json are in a subdirectory because the function will get the name of this directory)
Run the release and now the Azure resource group will be created and the function will be deployed to the resource group. Open your browser and call the function.
Success! Our Azure function works perfectly!
Hopefully this blogpost helps you in building Azure functions using precompiled assemblies which a lot of people don’t know is possible yet.
What kind of serverless development on Azure do you prefer? CSX or precompiled and why?
Geert van der Cruijsen
The post Building, testing and deploying precompiled Azure Functions appeared first on Mobile First Cloud First.
Look at our consultancy services, training offers and careers below or contact us at firstname.lastname@example.org