20

I want to use some shared classes on my Azure Functions to not duplicate code.

I have tried to create a empty C# function and create the classes inside the function and then import to the other functions with:

#r "../Shared/Class.cs"

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Luís Fura
  • 263
  • 1
  • 3
  • 6

4 Answers4

31

First, put your shared code inside a folder in the root of your Function App directory (e.g. "Shared"). Let's say I put a shared Message.csx class in that folder (e.g. full path D:\home\site\wwwroot\Shared\Message.csx).

To include this into your function use the #load command:

#load "..\Shared\Message.csx"

using System;
using Microsoft.Azure.WebJobs.Host;

public static void Run(Message message, TraceWriter log)
{
    log.Info($"C# Queue trigger function processed message: {message.Id}");
}

See the help page here for more information. By default, the files in that directory won't be tracked for changes. If you want to ensure that when files in that directory change your functions will pick up the changes and be recompiled, you can add your "Shared" directory to the watchDirectories list in host.json. E.g.:

{
    "watchDirectories": [ "Shared" ]
}
mathewc
  • 13,312
  • 2
  • 45
  • 53
6

Due to the rate of change within Azure Functions, this is no longer the recommended approach for C# functions( see Azure Functions Tools Roadmap). Refer to the following blog posts for depictions of the most modern and efficient patterns for structure a C# project in visual studio, and get all the advantages of shared DLL's the way you normally do in C#.

https://azure.github.io/AppService/2017/03/16/Publishing-a-.NET-class-library-as-a-Function-App.html

https://github.com/devkimchi/Precompiled-Azure-Functions-Revisited

Kristian Damian
  • 1,360
  • 3
  • 22
  • 43
solvingJ
  • 1,321
  • 1
  • 19
  • 30
  • The linked blog only describe the publishing .NET class library as a Function App as an alternative way "for customers who feel that that class libraries are a better fit for their application".However tools are not ready yet and "you can try precompiled functions today with a few manual tweaks to your project". Thank you for sharing the links, but I disagree with your unconditional "the most modern and efficient patterns for structure a C# project" – Michael Freidgeim May 20 '17 at 02:30
  • Indeed, I could have worded it more carefully by giving it proper context. So, more specifically, using multiple shared classes and nuget dependencies in an Azure Function App using CSX scripts in the portal quickly becomes a challenge as a Function App gets more complex. The OP seemed to be just starting to deal with these challenges. As a result, many have found many advantages to starting FunctionApps off as ASPNET Web Apps. This is now recommended by Microsoft and at least one blogger, and details can be found in the links above for those who wish to pursue this path. – solvingJ May 20 '17 at 03:41
4

Let me answer this question in a more human-understandable way taking into account that Azure Functions are new and don't have proper documentation yet.

Let's go step by step.

  1. You need to go to the Azure function "Platform features" section.

    enter image description here

  2. Then Navigate to Development tools->Advanced tools:

    enter image description here

  3. Next, navigate to Tools->Zip Push Deploy:

    enter image description here

  4. Next, create a folder called "Shared" inside root folder as it recommends in Microsoft documentation:

    enter image description here


Inside this folder, you can also create additional folders, classes if you want, for example, if you want to reuse Model classes between Azure functions then create an additional folder called "Models" and put your desired class there.

enter image description here

  1. After the creation of *.csx file you can edit it and put your code there and save:

    enter image description here

  2. Then reuse the class inside you Azure function by loading it using #load:

    enter image description here


Note:
Yet another way is to use Visual Studio with Microsoft DevOps CI/CD. There it will be much straightforward and easy to do the same.

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
0

When working from Visual Studio and looking for a way to share some C# script files between functions within your Function App like this:

#load "..\Shared\MySharedCode.csx"

Please be aware that you should set the 'Copy to Output Directory' property foor the files in your shared folder to 'Copy always'.

M. Veenman
  • 33
  • 8