Devs Night Club

Thoughts about developing

En #Microsoft trabajamos desde donde queremos y con la mejor compañía. #martesdemovientoenel7 (at Microsoft Colombia)

Two #beauties in my #glasses. #paris #eiffeltower #summer #europe #vacations #beard (at Tour Eiffel)
No todo lo bello tiene cimientos tradicionales.
.
Una #plaza llena de bellezas arquitectonicas. Al fondo, la torre inclinada de pisa. Un campanario construido sobre la arena.
.
.
.
.
.
#piazzamiracoli #pisa #italy #pisatower #architecturelovers...
Only looking forward, you’ll find new destinies.
#pontesantatrinita as seen from #pontevecchio.
.
.
.
.
.
#travel #travels #traveler #travelpic #travelers #travelphotography #travelblogger #travelgram #travelphoto #travelpics #travelphotos...
#Ruinas de #Pompeya en una gran tarde de #verano.
#pompeii #ruins #archeology #history #architecturelovers #architecture #architectureporn #sky #cloudporn #skyporn #italy #ancient #summer #vacations #travelphotography #travel (at Scavi di Pompei)
#Seattle vista desde el observatorio de la torre Smith. Composición de 14 fotogramas.
#panorama #landscape #cloudporn #skyline # city #citylights (at Smith Tower)

How to import External DLL’s and Read Local Files from Azure Functions

DllLocalFilesFunction

This sample shows how to use external assemblies in Azure functions, as well as how to read local files with Azure Functions.

Including external assemblies in Azure Functions

As you can notice there are two project folders inside this solution:

  1. CustomTypes
  2. DllLocalFileFunction

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:

  1. function.json
  2. project.json
  3. run.csx

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.

Reading local files from Azure Functions

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!!!.

Testing the function

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!!!." 
Let’s grow a #Beard
#911 #memorial #macro #yellow #rose #nyc