[Azure] Using precompiled DLL’s for your Functions

One of the cool things about Azure Functions is that they are very easy to get started. You create a new function, type some code and you’re off. This is very nice from a getting started point of view, but once you’re considering to use them for more than just playing around, other things come into play. For instance, you might want to actually test what you’re doing. You might want to reference projects, you might want to reuse some of the code you (or your company) already has. Now there’s all kinds of ways of doing this, but just recently the Function teams introduced another very interesting possibility: the use of precompiled DLL’s.

This means that instead of writing the code in one (or multiple) .csx files, you can use good-old DLL’s instead. Basically you only need to do two things:

  1. Provide an entry point for the Function to be executed. This is just like the Run method in a normal .csx file.
  2. Point the function.json configuration to the DLL which needs to be used. This is done using the scriptFile and entryPoint properties.

The function.json will look like this:

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

And the matching code file would look like this:

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);
        }
    }
}

I shamelessly copy/pasted this example from this article on the function GitHub wiki since it’s a nice simple example to start with. The matching GitHub repo is found here. It does not clearly state where to put the DLL. Don’t put it in a bin folder like you would with reference files, you can simply store it in the function folder itself.

Apart from the already mentioned benefits, this also means that you can now make a single DLL with multiple Run methods as entry points. Your would then create multiple Functions with a function.json each, all pointing to different entry points in your DLL. Although powerful; this does require some proper planning and architecture, a mistake is easily made!

I would like to add one little caveat: I found out that when running these things locally, you need to be very aware of the version of the functions cli you’ve got installed. That’s easily overlooked, just remember that these things are still in development so fixes and new features get pushed very often. That leads to the situation where the bits you’ve got on your box might not support the latest and greatest. To install the last version of the CLI, run npm install azure-functions-cli -g. When running func, the output should resemble this:

funcwindow

,

Related posts

Long Term Support… or not?

One of the cool things about Azure Functions is that they are very easy to get started. You create a new function, type some code and you're off. This is very nice from a getting started point of view, but once you're considering to use them for more than just playing around, other things come into play. For instance, you might want to actually test what you're doing. You might want to reference projects, you might want to reuse some of the code you (or your company) already has. Now there's all kinds of ways of doing this, but just recently the Function teams introduced another very interesting possibility: the use of precompiled DLL's.

[DevOps] Should you migrate onto YAML release pipelines?

One of the cool things about Azure Functions is that they are very easy to get started. You create a new function, type some code and you're off. This is very nice from a getting started point of view, but once you're considering to use them for more than just playing around, other things come into play. For instance, you might want to actually test what you're doing. You might want to reference projects, you might want to reuse some of the code you (or your company) already has. Now there's all kinds of ways of doing this, but just recently the Function teams introduced another very interesting possibility: the use of precompiled DLL's.

Latest posts

Long Term Support… or not?

One of the cool things about Azure Functions is that they are very easy to get started. You create a new function, type some code and you're off. This is very nice from a getting started point of view, but once you're considering to use them for more than just playing around, other things come into play. For instance, you might want to actually test what you're doing. You might want to reference projects, you might want to reuse some of the code you (or your company) already has. Now there's all kinds of ways of doing this, but just recently the Function teams introduced another very interesting possibility: the use of precompiled DLL's.

[DevOps] Should you migrate onto YAML release pipelines?

One of the cool things about Azure Functions is that they are very easy to get started. You create a new function, type some code and you're off. This is very nice from a getting started point of view, but once you're considering to use them for more than just playing around, other things come into play. For instance, you might want to actually test what you're doing. You might want to reference projects, you might want to reuse some of the code you (or your company) already has. Now there's all kinds of ways of doing this, but just recently the Function teams introduced another very interesting possibility: the use of precompiled DLL's.

Leave a Comment

Leave a Reply

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