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:

2 Replies to “Creating your first Precompiled Azure Function”

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.