![](/img/trans.png)
[英]How can I publish an exectuable file to my Azure Function from Visual Studio?
[英]Including a file when I publish my Azure function in Visual Studio
我知道這看起來很簡單,但我在網上找不到任何幫助。
當我使用 Visual Studio 發布它時,我想在我的 Azure 函數中包含一個文件 (.html)。 然后我希望能夠在我的 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 項目的根目錄中添加了該文件。 如果您改為將其添加到某個Data
文件夾中(更好的做法),您可以這樣寫:
string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "Data", "test.html");
有關完整的工作示例,請參見此處。
我看到我可以在Azure功能中使用發送網格,但看起來我只能發送一封電子郵件而不是多封電子郵件,這就是我想要的。
在使用SendGrid期間,我們還可以使用Azure功能發送多封電子郵件。 我們可以使用ICollector作為輸出。 我們可以使用Microsoft.Azure.WebJobs.Extensions.SendGrid SDK來做到這一點。
以下是演示代碼。
using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
namespace FunctionApp1
{
public static class TestSendGrid
{
[FunctionName("TestSendGrid")]
public static void Run([TimerTrigger("0 0 */1 * * *")]TimerInfo myTimer, TraceWriter log, [SendGrid(ApiKey = "sendGridKey", From ="send email")]ICollector<Mail> mails)
{
log.Info($"C# function executed at: {DateTime.Now}");
for (int i = 0; i < number; i++) // you could use your own logic
{
Mail message = new Mail()
{
Subject = $"multiple mails from SendGrid with Azure function !"
};
var personalization = new Personalization();
personalization.AddTo(new Email("sunguiguan@hotmail.com"));
Content content = new Content
{
Type = "text/plain",
Value = $"Hello Azure SendGrid!{i}"
};
message.AddContent(content);
message.AddPersonalization(personalization);
mails.Add(message);
}
}
}
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "xxxx",
"AzureWebJobsDashboard": "xxxx",
"sendgridapikey": "xxxxx"
}
}
我有和你一樣的場景。 但是,我無法訪問ExecutionContext
因為它僅在請求中可用。 我的場景需要獲取包含在 AzFunc 項目中的模板,而不是在 AzFunc 函數的上下文中。 我知道了null
當我與界面去-實現類的方法。
感謝這個人,我在課堂上使用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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.