【问题标题】:SendGrid not found in HTTPTrigger function在 HTTPTrigger 函数中找不到 SendGrid
【发布时间】:2018-01-17 15:06:40
【问题描述】:

我正在尝试创建一个 Azure 函数,它将在 macOS 中使用 VSCode 由 HTTP 触发。

使用官方documentation,我在本地机器上成功创建了一个HTTP Trigered azure函数。

现在我想在这个函数期间发送一封电子邮件,但是当我添加一个 senGrid 引用时,我得到了一个编译错误:

以下1个函数有误: [15/01/2018 21:51:12] ResetPasswordSendEmail:C# 编译服务错误:无法加载文件或程序集“SendGrid,Culture=neutral,PublicKeyToken=null”。该系统找不到指定的文件。 [15/01/2018 21:51:12]。无法加载文件或程序集“SendGrid,Culture=neutral,PublicKeyToken=null”。系统找不到指定的文件。

天蓝色功能:

#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

【问题讨论】:

  • 我觉得你需要参考NuGet包Microsoft.Azure.WebJobs.Extensions.SendGrid
  • 在哪里?我没有看到任何 csproj 并且手动添加 project.json 不能解决问题

标签: c# azure azure-functions


【解决方案1】:

由于您是在 Mac 上进行本地开发,因此您可能使用的是 v2“核心”版本的 func CLI 工具。

导航到您的项目根文件夹(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("you@gmail.com");
    message.AddContent("text/html", "Test body");
    message.SetFrom(new EmailAddress("test@test.com"));
    message.SetSubject("Subject");

    return new OkObjectResult("OK");
}

【讨论】:

  • 这是我的第一个天蓝色函数。所以我肯定错过了很多信息,感谢安装扩展。我正在尝试安装扩展并重建功能
  • @OrcusZ 你的bin文件夹和function-extensions文件夹中有SendGrid.dll吗?
  • 我将它安装在我的函数项目而不是我的函数文件夹中并且它可以工作。谢谢:D
【解决方案2】:

根据您的代码和描述,您可能会在门户网站和 VScode 上混合创建 sendgrid。

这里我创建了HttpTrigger函数并在portal上添加了sendgrid,你可以参考下面的代码:

天蓝色功能:

#r "SendGrid"
using SendGrid.Helpers.Mail;
using System.Net;

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out Mail message)
{
    log.Info("C# HTTP trigger function processed a request.");
    message = new Mail
    {        
        Subject = "Azure news"          
    };

    var personalization = new Personalization();
    personalization.AddTo(new Email("example@test.com"));   

    Content content = new Content
    {
        Type = "text/plain",
        Value = "msp"
    };
    message.AddContent(content);
    message.AddPersonalization(personalization);
    return req.CreateResponse(HttpStatusCode.OK,"hello");
}

函数json:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "sendGrid",
      "name": "message",
      "apiKey": "sendgridkey",
      "from": "example2@test.com",
      "subject": "hello",
      "text": "hello sendgrid",
      "direction": "out"
    }
  ],
  "disabled": false
}

有关更多详细信息,您可以阅读此article 以了解如何在 Azure Functions 中使用 sendgrid。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多