使用SendGrid的Azure函数未在Visual Studio中进行编译

时间:2018-05-20 22:38:50

标签: azure azure-functions sendgrid

我试图在Visual Studio中使用SendGrid运行Azure函数的简单示例:

[FunctionName("SendEmail")]
public static void SendEmail([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] [SendGrid()] out Mail message)
    {
        message = new Mail
        {
            Subject = "From VS"
        };

        var personalization = new Personalization();
        // change to email of recipient
        personalization.AddTo(new Email("joe@foo.com"));

        Content content = new Content
        {
            Type = "text/plain",
            Value = "testing from VS."
        };

        message.AddContent(content);
        message.AddPersonalization(personalization);
}

但是我收到了以下编译错误:

  

System.IO.FileNotFoundException:无法加载文件或程序集   ' Microsoft.Azure.WebJobs,Version = 2.2.0.0,Culture = neutral,   公钥= 31bf3856ad364e35'或其中一个依赖项。该   系统找不到指定的文件。

除了这些警告: enter image description here

我安装了Visual Studio 2017 15.7.1。该项目的目标是.NET Standard 2.0,我的项目安装了以下NuGet包:

enter image description here

我应该安装或删除哪些内容才能进行编译?

2 个答案:

答案 0 :(得分:1)

在引用 Microsoft.Azure.WebJobs.Extensions.SendGrid版本2.2.0 包时,我在下面进一步观察了构建警告,因为它与.NET Standard不兼容。将软件包升级到3.0.0-beta5版后,警告消失了。此外,请注意这意味着使用测试版产品,在发布任何生产代码之前对其进行彻底测试。

 1. 'Microsoft.Azure.WebJobs.Extensions.SendGrid 2.2.0' was restored   
    using '.NETFramework,Version=v4.6.1' instead of the project target  
    framework '.NETStandard,Version=v2.0'. This package may not be fully
    compatible with your project.
 2. 'Sendgrid 8.0.5' was restored using '.NETFramework,Version=v4.6.1'
    instead of the project target framework '.NETStandard,Version=v2.0'.
    This package may not be fully compatible with your project.
 3. 'SendGrid.CSharp.HTTP.Client 3.0.0' was restored using
    '.NETFramework,Version=v4.6.1' instead of the project target
    framework '.NETStandard,Version=v2.0'. This package may not be fully
    compatible with your project.

答案 1 :(得分:1)

正如Evandro Paula所说,你可以Install-Package Microsoft.Azure.WebJobs.Extensions.SendGrid -Version 3.0.0-beta5让它与.net标准兼容。

然后,您的脚本似乎是v1和v2函数的混合。例如,CreateResponse()不属于HttpRequestMail类不属于SendGrid汇编。

以下是从HTTP函数发送邮件的一个非常简单的示例:

public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, [SendGrid(ApiKey ="sendgridkey")] out SendGridMessage message, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            message = new SendGridMessage();
            message.AddTo("testto@gmail.com");
            message.AddContent("text/html", "Test body");
            message.SetFrom(new EmailAddress("testsend@gmail.com"));
            message.SetSubject("Subject");

            return new OkObjectResult("OK");
        }

local.setting.json文件中设置sendgridkey:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "storageconnectionstring",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "sendgridkey": "yoursendgridkey"
  }
}