External assemblies in Azure Functions

TL;DR: If your code depends on some external assemblies, and these are not on Nuget, you can still use them, they just need to be added to the files you put on Azure. And when we take a closer look at what a Azure Function is, you might be in for a surprise.


The problem

You have some dependency on an external assembly, and it's not on Nuget. It might be your own code, which is othen the case for me. I'm not that comfortable with C# script just yet, and I can't get intelli-sense working in Visual Studio or VSCode for the csx files (there's a topic for a future blog post). Or you might just, like me, like to test the code, out of environment, say on your CI setup.

So how do you include your own assemblies?

The solution

In the earlier post on getting work done in the cloud, you might have noticed that I'm referencing a couple of external assemblies. This is almost how's it done with your own assemblies. You just need to tell the function where to locate these assemblies.

So, I'm using source control for my functions, if you're not, we'll get to that in a bit.

In the folder where you have the run.csx, project.json and function.json files you create a bin folder, and drop your assemblies there. Then you add a line for each assembly in the run.csx file:

#r "bin\<assembly-name>.dll"

Where you, of course, put in the name of your assembly, and remember to put in a using line too.

Then you add the assemblies to source control, commit and push.

For those not using source control, in the Azure portal UI for function, you can click the 'view files' link just below the code editor, and you can upload files to the folder of the function.

So I got that working, then I added another function to the same repository (a new folder, with another run.csx, function.json and project.json file), added another bin folder with some assemblies. Commit, push, and refresh the Azure portal.

but...

Then I start getting errors. I update an assembly, commit and push, but the code still can't locate the new method in the assembly. And one of my assemblies fail loading one of the external (nuget) assemblies in the correct version. The exception is about loading version 1.0.0, and the reference I have, both in my Visual Studio project, and in the project.json file, is to version 1.3.0. So what gives?

I change both run.csx and project.json, commit, push, and only one change seems to be recognized by the UI, and the code fails.

Conclusion

It might be me, or it might actually be a real issue. Maybe I've run into some bugs, maybe I'm doing it wrong, maybe because I haven't found the right articles, maybe because they're not written yet.

I've got five functions running so far, one timer triggere, one blob triggere and 3 queue triggered. They all work as intented, but I've been forced to put way more code into the run.csx file than I like. Four of the triggers are all part of the same solution, and I would have liked for them to use the assemblies I'm using in the rest of the (web) solution, with code shared between them, where it makes sense. But I've given up on that! For now at least.

The surprise

This might not come as a surprise for you, but it was a bit unexpected for me. Some of the errors came with the full path to the functions, and it turns out that the functions reside in a folder structure below wwwroot. So a function (or set of functions) are actually some sort of (web) app? Like so many other services on Azure it seems!

Comments

Thank you for your comment! If it's not spam, it will be published shortly!

Your e-mail address will not be published. Required fields are market *

*

Copyright © 2015 by Steen F. Tøttrup. Powered by NBlogEngine by creativeminds & Romangie Theme.