Referencing external assemblies in Azure Functions
I have previously blogged about Azure Functions and the potential of these small, stand-alone functions. It is a welcome addition to the Microsoft’s micro services vision. Function Apps are about the smallest amount of work you can do and it’s all easily exposed through an API.
You can code Node.JS if you want, but if you use C#, you have the entire .Net framework at your disposal – not everything out-of-the box though – and you are also able to include NuGet packages in your Function Apps. Which is pretty neat. But what about 3rd party libraries that are not packaged using NuGet?
Good news! You can reference external assemblies and thus any 3rd party library you want. Here’s how.
Adding an external assembly
Let’s first create a new assembly. I am using a very complex calculation to determine the answer to Life, The Universe and Everything. I don’t want to clutter the Function App, so I created a 3rd party SuperComputer class to handle this complex logic.
public class SuperComputer { public string GiveTheAnswerToLifeTheUniverseAndEverything() { int answer = 1 * 0 * 2 * 0 * 3 * 0 * 4 * 0 * 5 * 0 * 6 * 0 * 7 * 0 * 8 * 0 * 9 * 0 * 10 * 0 * 11 * 0 * 12 * 0 * 13 * 0 * 14 * 0 * 15 * 0 * 16 * 0 * 17 * 0 * 18 * 0 * 19 * 0 * 20 * 0 * 21 * 0 * 22 * 0 * 23 * 0 * 24 * 0 * 25 * 0 * 26 * 0 * 27 * 0 * 28 * 0 * 29 * 0 * 30 * 0 * 31 * 0 * 32 * 0 * 33 * 0 * 34 * 0 * 35 * 0 * 36 * 0 * 37 * 0 * 38 * 0 * 39 * 0 * 40 * 0 * 41 * 0 + 42 + 0; return answer.ToString(); } }
Now let’s get this super computer class up in the cloud, in our Function App. The functionality is included in WhatIsTheAnswerToLifeTheUniverseAndEverything.dll.
Since Azure Function Apps run as an App Service, it’s actually pretty easy to use the code. It can be done in 3 easy steps:
- Add a bin folder relative to your Function App
- Add the DLL in the bin folder
- Reference the DLL from your CSX script
From your Function App you can go to your App Service settings to find your (S)FTP server settings.
After that, find your favorite FTP client and browse to your Function App. To be able to use the assembly, you need to put it in the bin directory, which is probably not there. Create the bin directory and copy the dll into the bin folder.
Now that the DLL is in place, let’s start using the super computer and finally get the answer we’ve all been waiting for…
Referencing the assembly
Now for the hard part 😉 At least for me it was. I couldn’t find the right commando/keyword, since I’d never done this before. I finally found it through msdn. So #load is for to include external *.csx files, #r is for including external assemblies.
Adding the assembly we just uploaded means adding #r “WhatIsTheAnswerToLifeTheUniverseAndEverything.dll” at the top of the code in the Function App. After some more editing, I ended up with this hyper powered, super computed, unthreaded, synchronous Function App:
#r "WhatIsTheAnswerToLifeTheUniverseAndEverything.dll" using System.Net; using WhatIsTheAnswerToLifeTheUniverseAndEverything; public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log) { log.Verbose($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}"); var superComputer = new SuperComputer(); var answer = superComputer.GiveTheAnswerToLifeTheUniverseAndEverything(); return req.CreateResponse(HttpStatusCode.OK, "Tough one. I had to use an external assembly for this one, but I didn't know how to use it at first. Had to look into some stuff and I finally managed. The answer is '" + answer + "'"); }
As thrilled as I am, I wanted to know the answer. I could have used the testform, but since I put the long sentence in, it was hard to get it visualized over here, hence Postman:
So that turned out to be very easy once you know how to do it. I just had a hard time finding the documentation, maybe other people had more luck, or will come across this blogpost in the future 🙂
More information on references
More information on using external assemblies, Nuget and csx-files can be found here on msdn:
https://azure.microsoft.com/en-us/documentation/articles/functions-reference-csharp/
Great post! Helped me out a lot!
Thanks!
Thank You Sir. Worked for me. I was using Microsoft.WindowsAzure.MediaServices.Client
Thanks this help me load some libraries that I needed.
I guess the time of the article the upload feature wasn’t available?
Nope. They add features monthly, so mostly posts are deprecated two weeks after posting 🙂
Awesome.. thanks for sharing