Precompiled functions

Fabio Cavalcante edited this page Apr 11, 2018 · 5 revisions

NOTE: The pre-compiled model documented here has been deprecated and will be removed in the next major version of Azure Functions (2.0). This model has been replaced by the direct load, attributed model used by Visual Studio by default.

Azure Functions supports precompiled functions, which enables the use of .NET assemblies containing the function implementation, bypassing the dynamic compilation process.

In order to use precompiled functions, your function.json file must have the scriptFile and entryPoint properties set, pointing to the assembly file and fully qualified method name, respectively:

Sample function.json

{
    "scriptFile": "PreCompiledFunctionSample.dll",
    "entryPoint": "PreCompiledFunctionSample.MyFunction.Run",
    "bindings": [
        {
            "authLevel": "function",
            "name": "req",
            "type": "httpTrigger",
            "direction": "in"
        },
        {
            "name": "$return",
            "type": "http",
            "direction": "out"
        }
    ],
    "disabled": false
}

NOTE: You can deploy your assembly to other locations and use a relative path from the function folder in your scriptFile path (e.g. ..\\bin\\PreCompiledFunctionSample.dll)

Sample function implementation

using System.Net;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;

namespace PreCompiledFunctionSample
{
    public class MyFunction
    {
        public static async Task<HttpResponseMessage> Run(HttpRequestMessage req)
        {
            // parse query parameter
            string name = req.GetQueryNameValuePairs()
                .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                .Value;

            // Get request body
            dynamic data = await req.Content.ReadAsAsync<object>();

            // Set name to query string or body data
            name = name ?? data?.name;

            return name == null
                ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
                : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
        }
    }
}

With this approach, functions can be developed as a regular class library, using any .NET language. For the above sample, the function folder contained the function.json and the assembly file, PreCompiledFunctionSample.dll

Here are some sample projects:

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.