问题描述
我想在我的 Azure Functions 上使用一些共享类来避免重复代码.
I want to use some shared classes on my Azure Functions to not duplicate code.
我尝试创建一个空的 C# 函数并在函数内部创建类,然后导入到其他函数:
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"
推荐答案
首先,将您的共享代码放在 Function App 目录根目录下的文件夹中(例如Shared").假设我在该文件夹中放置了一个共享的 Message.csx
类(例如完整路径 D:homesitewwwrootSharedMessage.csx
).
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:homesitewwwrootSharedMessage.csx
).
要将其包含到您的函数中,请使用 #load
命令:
To include this into your function use the #load
command:
#load "..SharedMessage.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}");
}
查看帮助页面此处了解更多信息信息.默认情况下,不会跟踪该目录中的文件的更改.如果您想确保当该目录中的文件发生更改时,您的函数将获取更改并重新编译,您可以将共享"目录添加到 host.json<中的
watchDirectories
列表中/代码>.例如:
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" ]
}
这篇关于Azure Functions - 共享类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!