我正在尝试创建一个 Azure Function,它将在 macOS 中使用 VSCode 通过 HTTP 触发。
使用官方documentation ,我在本地计算机中成功创建了 HTTP Trigered azure 函数。
现在我想在此功能期间发送电子邮件,但是当我添加 senGrid 引用时,出现编译错误:
The following 1 functions are in error: [15/01/2018 21:51:12] ResetPasswordSendEmail: C# compilation service error: Could not load file or assembly 'SendGrid, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. [15/01/2018 21:51:12] . Could not load file or assembly 'SendGrid, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
azure 函数:
#r "SendGrid"
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using SendGrid.Helpers.Mail;
public static IActionResult Run(HttpRequest req, TraceWriter log, out Mail message)
{
log.Info("C# HTTP trigger function processed a request.");
string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string email = data.email;
string password = data.password;
message = new Mail
{
Subject = "Azure news"
};
bool isOk = !string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password);
return isOk ? req.CreateResponse(HttpStatusCode.OK) : req.CreateResponse(HttpStatusCode.BadRequest, "EmailError");
}
函数 json :
{
"disabled": false,
"bindings": [{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in"
},
{
"name": "message",
"type": "sendGrid",
"direction": "out",
"apiKey": "AzureWebJobsSendGridApiKey"
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
]
}
我希望将代码保留在同一位置,并且不使用代码管理 (GIT) 之外的 azure 函数。
如果有人知道如何解决我的问题,请帮忙:D
最佳答案
由于您是在 Mac 上进行本地开发,因此您可能使用的是 func
CLI 工具的 v2“核心”版本。
导航到项目根文件夹(其中 host.json
所在)并执行以下命令:
func extensions install -p Microsoft.Azure.WebJobs.Extensions.SendGrid -v 3.0.0-beta4
这会将 SendGrid
二进制文件添加到 Function App 的 bin
文件夹中。
然后,您的脚本似乎是 v1 和 v2 函数的混合。例如,CreateResponse()
不属于 HttpRequest
,并且 Mail
类不在 SendGrid
程序集中。下面是一个从 HTTP 函数发送邮件的非常简单的示例:
public static IActionResult Run(HttpRequest req, TraceWriter log, out SendGridMessage message)
{
log.Info("C# HTTP trigger function processed a request.");
message = new SendGridMessage();
message.AddTo("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b12041e2b0c060a020745080406" rel="noreferrer noopener nofollow">[email protected]</a>");
message.AddContent("text/html", "Test body");
message.SetFrom(new EmailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e5a4b5d5a6e5a4b5d5a004d4143" rel="noreferrer noopener nofollow">[email protected]</a>"));
message.SetSubject("Subject");
return new OkObjectResult("OK");
}
关于c# - 在 HTTPTrigger 函数中找不到 SendGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48271272/