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 Funtion, 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 directroy and place the assembly withn 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.

Comments (7)

  1. Shyju says:

    Glad i found this article. This is nice stuff. Thanks for jotting this down!!!

  2. dafna says:

    Is there a better way than writing a hard coded path..?
    #r “D:\home\site\wwwroot\GreetingsAssemblyReference\bin\benjamin.dll”

    What is MS decide to change this path…?

    1. @Dafna you should be able to use a relative path, something like “\GreetingsAssemblyReference\bin\benjamin.dll”. I did not test that, I mention it in the post, but haven’t tested it. HTH

    2. I have tested with

      #r “.\bin\MyDll.dll”

      and it worked.

  3. Great article, I was having this exact problem. If I may make a suggestion, the article should be more specific on the way to upload the dll. Since the interface provides no button, I dragged and dropped and it worked, but it’s not obvious by reading the article.
    Thanks!

  4. Andy says:

    I’ve used this with a Webhook + API – drag and drop the dll into wwwroot\bin
    and the path reference is
    #r “..\bin\libName.dll”

    because it looks like the function is running a folder lower than wwwroot, hence the up-ref “..”

Skip to main content