This sample shows how to use external assemblies in Azure functions, as well as how to read local files with Azure Functions.
As you can notice there are two project folders inside this solution:
The first one is intended to serve as a sample of an external component. It just contains a class with an abstraction of the complex numbers: ComplexNumber.cs
. We are going to use it to create a complex number and then use it overrided ToString()
to write it down to the response of the function.
Compiling this component will produce the CustomTypes.dll
assembly, and it will be imported in the function problem.
The second project is the one containing the function. An Azure Function project can contain one or more functions. Each one of them will be included in its own folder. We have named the only function inside the project as DllLocalFileFunction
. Inside this folder you can find specially:
In this sample it is of special interest the run.csx
file as it is the one containing the code that will use the functionality defined in the external component (CustomTypes)
.
Please note that in order to you can reference the external assembly a folder named bin
have been created inside the function folder. This folder was created manually. It is not generated when you create the function using the Visual Studio Tools for Azure Functions. Then inside this folder, you mustinclude the generated assembly CustomTypes.dll
and after this has been done, you must declare the reference in the functions code, at the beginning of the file:
#r "CustomTypes.dll"
And then, add the traditional using
directive to include the namespace inside the assembly you will be using:
using CustomTypes;
With this done, you can simply call:
ComplexNumber complex = new ComplexNumber(3, 5);
Note that the IntelliSense provided by Visual Studio may not give you information about this class, as the tooling is still in beta.
This is not a recommended practice, as in most cases if your Function needs to read files it will be better that those files would be stored on an Azure Blob, and downloaded as required.
But if for any reason you need to complement your function with a companion file (maybe a custom configuration XML file), you can read them using the traditional I/O libraries provided by the .NET Framework:
using System.IO;
...
...
var fileText = File.ReadAllText("./DllLocalFileFunction/hello.txt");
Note that you just pass the relative path of the file: . for root, DllLocalFileFunction
as the function folder, and then the file that we have included (in this case, a simple hello.txt
file containing the text Hello World!!!
.
As this function is based on the Generic WebHook
template, it receives a name
parameter in the get
call. Then it returns a greeting for the name, the complex number created, and the content fot the file. For example if you have:
You could use CURL
from the bash console to test the function after you have launched it from Visual Studio: (note that a console application will be launched and act as the HTTP server for you to test from development)
root@UNDERBEAST:/mnt/c/Users/warnov# curl http://localhost:7071/api/DllLocalFileFunction?name=warnov
Answer should be:
"Hello warnov here is a complex number 3+5i. And the text content is: Hello World!!!."