2

Creating your first Precompiled Azure Function

Since a couple of weeks it is possible to create Precompiled Azure Functions.  This enables the use of .NET assemblies containing the function implementation, and bypassing the dynamic compilation process.

Precompiled Function

In order to use precompiled functions, you need to create a .Net assembly and adjust the function.json file. First of create a class library within Visual Studio  and add the following class:

using Microsoft.Azure.WebJobs.Host;

namespace MsftPlayground.Function {
    public class LoggingFunction
    {
        public static void Run(string input, TraceWriter log) {
            log.Info($"Manually triggered PRECOMPILED function: {input}");
        }
    }
}

For this to work and compile the “Microsoft.Azure.WebJobs” NuGet packages needs to be added to your project.

Once the function method it self is able to compile we can create the “function.json” file. Important for this file is:

  • scriptFile: The file that contains the function.
  • entryPoint: The fully qualified method name.

All of these options together create the following “function.json” file for a manually triggered function.

{
  "scriptFile": "MsftPlayground.Function.dll",
  "entryPoint": "MsftPlayground.Function.LoggingFunction.Run",
  "disabled": false,
  "bindings": [
    {
      "type": "manualTrigger",
      "direction": "in",
      "name": "input"
    }
  ]
}

Azure Function

No that we have the correct files in place we need to get this up and running in Azure.

  1. Open the Azure Portal (https://portal.azure.com)
  2. Navigate to the resource group where in you want to create the Function or create a new one.
  3. In the resource group click “Add” to create a “Function App”.

Create Azure Function App

  1. In the created Function App create a new function. In this example a C# Manually triggered function will be created.

Add Manual Trigger Function

  1. In the develop section of the function click on “View files”.

Azure Function Code

  1. In the file browser delete the “run.csx” file and adjust the “function.json” file as mentioned above.
  2. Now use the upload button to upload the compiled file.
  3. Click the “Run” button so see if your compiled function works.

Azure Function Function Json

The functionality looks very promising and make sure you bypass the dynamic compilation process. But there are a few things to keep in mind when the Azure Function:

  • When you make manual adjustments within the portal the function.json file will be refreshed (both scriptFile and entryPoint properties will be removed).
  • Compiled files will only be loaded the first time you run the function. When you make changes to the compiled file you will need to restart the function app before changes will be applied.

Restarting the complete Azure Function App can be done via:

  1. Open the Function App.
  2. Click on Function App settings.
  3. In the settings window click “Go to App Service Settings”
  4. In the overview blade click on “Restart”.

All source files are available on GitHub:

Related Posts

Azure DevOps Automation A couple of weeks ago the rename / rebranding of Visual Studio Team Services to Azure DevOps was announced. The rebranding is a great step forward int...
AKS (Kubernetes) and no connection could be made because the target machine acti... A client of my had an error while connecting to different resources within their Kubernetes cluster in Azure (AKS). Kubectl error On the kubectl com...
Kubernetes (AKS) attached to Azure Storage (Files) Kubernetes (AKS) can be used for many situations. For a client we needed to make files available trough a Kubernetes Pod. The files needed to be share...
Resource Group deployment via ARM templates When deploying an Azure Resource Manager (ARM) template you have to create a resource group within Azure. To deploy a template via script your script ...
Azure Managed Service Identity and Local Development Instead of storing user credentials of an external system in a configuration file, you should store them in the Azure Key Vault. Before MSI (Managed S...
The securitydata Azure resource group Most of the times companies have rules in place for managing their Azure environment. The main rules that should be in place are “Azure Policies” and ...

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.