How to add assembly references to an Azure Function App

I wanted to do this and I found these articles helpful: #AzureFunction #Azure #Csharp

It did take some hours, but I finally got it done.  Here is how I did it.

  • Create an Azure Function
  • Add the assembly to the BIN directory using KUDU
  • Include the assembly and code the Azure Function to use it
  • Test the Azure Function

Create an Azure Function

There is nothing overly exciting about this part, ok maybe it is exciting after all, create the Azure Function, you know the deal.

image

Figure 1, Create an Azure Function

I did create a Custom function, I.e. not a Timer, Data processing or Webhook + API one.  Instead I created an empty/default HttpTrigger-Csharp one, see Figure 2.

image

Figure 2, Create an Azure Function, default HttpTrigger

Add the assembly to the BIN directory using KUDU

I have discussed what SCM / KUDU is here.  Knowing that you can then access the SCM / KUDU console by adding the SCM to the URL shown in the Overview tab in the portal, as seen in Figure 3. Example: <webappname>.scm.azurewebsites.net

image

Figure 3, Accessing SCM / KUDU Azure Function

After you access SCM / KUDU for the Azure Function, navigate to the method you created and within it, create a BIN directory and place the assembly within it, as seen in Figure 4.  The creation of the benjamin.dll is describe here, referenced earlier.

image

Figure 4, Accessing SCM / KUDU Azure Function, add a BIN directory and the assembly

Include the assembly and code the Azure Function to use it

As seen in the code snippet below you include the assembly using the #r identifier.  You might be able to make it work using a relative path, but I included the absolute path and the name of the dll including the file extension.  Also add the using declaration so that the methods within the DLL can be accessed.  There was no intellisense, so I had to know the proper way to call the method already.

#r "D:\home\site\wwwroot\GreetingsAssemblyReference\bin\benjamin.dll"

using System.Net;
using benjamin;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
     string name = req.GetQueryNameValuePairs()
         .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
         .Value;

    var greetings = benjamin.Greetings.Hello(name);

    dynamic data = await req.Content.ReadAsAsync< object>();

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

Then I just made some simple changes to the default Azure Function to send back my Greetings and tested it using CURL as seen in Figure 5.

image

Figure 4, testing an assembly with an Azure Function, add a BIN directory and the assembly

And it worked just as expected.