问题描述
我想在Azure函数上使用一些共享类,以不重复代码.
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:\home\site\wwwroot\Shared\Message.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:\home\site\wwwroot\Shared\Message.csx
).
要将其包含在您的函数中,请使用#load
命令:
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}");
}
请参见帮助页面此处以获取更多信息信息.默认情况下,该目录中的文件不会被跟踪更改.如果要确保当该目录中的文件更改时,您的功能将接受更改并重新编译,则可以将共享"目录添加到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-共享的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!