在Visual Studio中发布Azure函数时包含一个文件 [英] Including a file when I publish my Azure function in Visual Studio
问题描述
我知道这似乎很简单,但是我无法在线找到任何帮助.
当我使用Visual Studio发布文件时,我想将文件(.html)与Azure函数一起包括在内.然后,我希望能够在我的Azure函数中访问此文件. 为什么?发布时似乎只有.dll被发送到服务器.
此文件将是.html文件,它将是电子邮件模板.我想在功能中阅读它,然后发送电子邮件.
非常感谢您的帮助.
我看到我可以使用[在Azure函数中发送网格] [1],但是看来我只能发送一封电子邮件,不能发送多封电子邮件,这正是我想要的.
首先,您需要将html文件添加到项目中,然后在属性中将复制到输出目录"设置为如果更新则复制".
然后在功能代码中输入一个附加的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");
有关完整的工作示例,请参见此处./p>
I know this seems like a simple thing but I can't find any help online.
I want to include a file (.html) along with my Azure function when I publish it using Visual Studio. Then I want to be able to access this file in my Azure function. Why? It seems like only the .dll gets sent to the server when I publish.
This file will be an .html file that will be an email template. I want to read it in my function and then send emails out.
Any help is much appreciated.
I see I can use [send grid in Azure functions][1], but it looks like I can only send out one email and not multiple emails, which is what I want.
First, you need to add the html file to your project, and in the properties, set Copy to Output Directory to "Copy if newer".
Then in your function code, take in an additional ExecutionContext context
parameter (note that this is Microsoft.Azure.WebJobs.ExecutionContext
and not System.Threading.ExecutionContext
). And when you need to access your html file, you can then write:
string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "test.html");
That's assuming you added the file at the root of your VS project. If you instead added it in some Data
folder (better practice), you'd write:
string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "Data", "test.html");
See here for full working sample.
这篇关于在Visual Studio中发布Azure函数时包含一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!