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.
- Open the Azure Portal (https://portal.azure.com)
- Navigate to the resource group where in you want to create the Function or create a new one.
- In the resource group click “Add” to create a “Function App”.
- In the created Function App create a new function. In this example a C# Manually triggered function will be created.
- In the develop section of the function click on “View files”.
- In the file browser delete the “run.csx” file and adjust the “function.json” file as mentioned above.
- Now use the upload button to upload the compiled file.
- Click the “Run” button so see if your compiled function works.
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:
- Open the Function App.
- Click on Function App settings.
- In the settings window click “Go to App Service Settings”
- In the overview blade click on “Restart”.
All source files are available on GitHub: