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.

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.

Screen Shot 2017-01-30 at 17.23.24

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.

  1. Select your solution file to restore nuget packages
  2. Select your solution file to build the solution
  3. Select your tests project assemblies so the build can execute the unit tests we created earlier.
  4. Publish symbols
  5. Copy the PreCompiledFunctionDemo.dll and the function.json file from the build output to a artifacts folder
  6. Copy the json files from the ARM template to the artifacts folder
  7. Publish the drop folder of the earlier copied artifacts

Screen Shot 2017-01-30 at 22.17.15

 

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.

Screen Shot 2017-01-30 at 22.57.33

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.

  1. Azure deploy step: select the ARM template azuredeploy.json file and parameters file to create a new Azure resource group.
  2. 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)

Screen Shot 2017-01-30 at 22.35.48

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.

Screen Shot 2017-01-30 at 22.48.44

Success! Our Azure function works perfectly!

Conclusion

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?

Happy coding!

Geert van der Cruijsen

Please follow and like:
RSS
Follow by Email
Facebook
Google+
https://mobilefirstcloudfirst.net/2017/01/building-testing-deploying-precompiled-azure-functions/
LinkedIn