Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions - Shared classes

Tags:

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"

like image 919
Luís Fura Avatar asked Sep 16 '16 22:09

Luís Fura


People also ask

Can multiple Azure functions use the same storage account?

It's possible for multiple function apps to share the same storage account without any issues. For example, in Visual Studio you can develop multiple apps using the Azure Storage Emulator.

Can Azure functions run in parallel?

Parallel executionWhen multiple triggering events occur faster than a single-threaded function runtime can process them, the runtime may invoke the function multiple times in parallel. If a function app is using the Consumption hosting plan, the function app could scale out automatically.

Can a function app have multiple functions?

Organize your functionsThese functions are often combined into a single function app, but they can also run in separate function apps. In Premium and Dedicated (App Service) hosting plans, multiple function apps can also share the same resources by running in the same plan.


1 Answers

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" ]
}
like image 112
mathewc Avatar answered Sep 18 '22 15:09

mathewc