在 Visual Studio 中发布我的 Azure 函数时包含一个文件
Including a file when I publish my Azure function in Visual Studio
我知道这看起来很简单,但我在网上找不到任何帮助。
我想在使用 Visual Studio 发布 Azure 函数时包含一个文件 (.html)。然后我希望能够在我的 Azure 函数中访问这个文件。
为什么?当我发布时,似乎只有 .dll 被发送到服务器。
此文件将是一个 .html 文件,它将成为电子邮件模板。我想在我的函数中阅读它,然后发送电子邮件。
非常感谢任何帮助。
我知道我可以使用 [Azure 函数中的发送网格][1],但看起来我只能发送一封电子邮件而不是多封电子邮件,这正是我想要的。
首先,您需要将html文件添加到您的项目中,并在属性中将复制到输出目录设置为"Copy if newer"。
然后在你的函数代码中,接受一个额外的ExecutionContext context
参数(注意这是Microsoft.Azure.WebJobs.ExecutionContext
而不是 System.Threading.ExecutionContext
)。当你需要访问你的 html 文件时,你可以这样写:
string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "test.html");
假设您将文件添加到 VS 项目的 root。如果您改为将它添加到某个 Data
文件夹中(更好的做法),您会写:
string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "Data", "test.html");
有关完整的工作示例,请参阅 here。
我遇到的情况和你一样。但是,我无法访问 ExecutionContext
,因为它仅在请求中可用。我的场景需要获取包含在 AzFunc 项目中但不包含在 AzFunc 函数上下文中的模板。当我使用界面 - 实现 class 方法时,我得到了它 null
。
感谢 this guy,我在 class 中使用 IOptions<ExecutionContextOptions>
来获取 Azure Func 的根目录。
我的 Azure Func 项目(NET 6,Azure Function v4)
using Microsoft.Extensions.Options;
using Microsoft.Azure.WebJobs.Host.Bindings;
namespace AzureFuncApi
{
public class TemplateHelper : ITemplateHelper
{
private readonly IOptions<ExecutionContextOptions> _executionContext;
public TemplateHelper (IOptions<ExecutionContextOptions> executionContext)
{
_executionContext = executionContext;
}
public string GetTemplate()
{
var context = _executionContext.Value;
var rootDir = context.AppDirectory; // <-- rootDir of AzFunc
var template = Path.Combine(rootDir, "test.html"); // <-- browse for your template. Here's an example if you place test.html right in the root of your project
// return your template here, raw, or after you do whatever you want with it...
}
}
}
我的不同项目定义了接口并在那里使用它,独立于实际实现
namespace DifferentProject
{
public interface ITemplateHelper
{
string GetTemplate(); // Use this to get the template
}
}
我知道这看起来很简单,但我在网上找不到任何帮助。
我想在使用 Visual Studio 发布 Azure 函数时包含一个文件 (.html)。然后我希望能够在我的 Azure 函数中访问这个文件。 为什么?当我发布时,似乎只有 .dll 被发送到服务器。
此文件将是一个 .html 文件,它将成为电子邮件模板。我想在我的函数中阅读它,然后发送电子邮件。
非常感谢任何帮助。
我知道我可以使用 [Azure 函数中的发送网格][1],但看起来我只能发送一封电子邮件而不是多封电子邮件,这正是我想要的。
首先,您需要将html文件添加到您的项目中,并在属性中将复制到输出目录设置为"Copy if newer"。
然后在你的函数代码中,接受一个额外的ExecutionContext context
参数(注意这是Microsoft.Azure.WebJobs.ExecutionContext
而不是 System.Threading.ExecutionContext
)。当你需要访问你的 html 文件时,你可以这样写:
string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "test.html");
假设您将文件添加到 VS 项目的 root。如果您改为将它添加到某个 Data
文件夹中(更好的做法),您会写:
string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "Data", "test.html");
有关完整的工作示例,请参阅 here。
我遇到的情况和你一样。但是,我无法访问 ExecutionContext
,因为它仅在请求中可用。我的场景需要获取包含在 AzFunc 项目中但不包含在 AzFunc 函数上下文中的模板。当我使用界面 - 实现 class 方法时,我得到了它 null
。
感谢 this guy,我在 class 中使用 IOptions<ExecutionContextOptions>
来获取 Azure Func 的根目录。
我的 Azure Func 项目(NET 6,Azure Function v4)
using Microsoft.Extensions.Options;
using Microsoft.Azure.WebJobs.Host.Bindings;
namespace AzureFuncApi
{
public class TemplateHelper : ITemplateHelper
{
private readonly IOptions<ExecutionContextOptions> _executionContext;
public TemplateHelper (IOptions<ExecutionContextOptions> executionContext)
{
_executionContext = executionContext;
}
public string GetTemplate()
{
var context = _executionContext.Value;
var rootDir = context.AppDirectory; // <-- rootDir of AzFunc
var template = Path.Combine(rootDir, "test.html"); // <-- browse for your template. Here's an example if you place test.html right in the root of your project
// return your template here, raw, or after you do whatever you want with it...
}
}
}
我的不同项目定义了接口并在那里使用它,独立于实际实现
namespace DifferentProject
{
public interface ITemplateHelper
{
string GetTemplate(); // Use this to get the template
}
}