无法在C#Azure功能中使用Nuget包

时间:2018-04-12 18:38:58

标签: c# azure azure-functions csx

我试图编写一个使用Sendgrid发送电子邮件的azure函数。但是,我无法识别外部nuget包的功能。这就是我所拥有的:

project.json

{
"frameworks": {
    "net46": {
        "dependencies": {
            "SendGrid": "9.9.0"
        }
    }
  }
}

run.csx:

using System;
using Sendgrid;

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    var client = new SendGridClient("xxx");
    var fromAddr = new EmailAddress("xxx@xxx.com", "xxx");
    var toAddr = new EmailAddress("xxxx", "xxx);
    var msg = MailHelper.CreateSingleEmail(fromAddr, toAddr, "subject", "content", "content");
    client.SendEmailAsync(msg).Wait();
}

我收到此错误:

[Error] run.csx(8,7): error CS0246: The type or namespace name 'Sendgrid' could not be found (are you missing a using directive or an assembly reference?)

我错过了什么?

1 个答案:

答案 0 :(得分:1)

如果您确实在v1运行时,那么您只是错过了一个具有EmailAddress类型的using语句。

将此添加到 -

using SendGrid.Helpers.Mail;

如果您使用的是v2(测试版/ .NET核心版),请点击评论中的 kim 的网址(you'll need a function.proj instead) -

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>  
    <ItemGroup>
        <PackageReference Include="SendGrid" Version="9.9.0"/>
    </ItemGroup>
</Project>

SendGrid NuGet package目标 .NET Standard 1.3 ,因此在.NET Core上运行应该没有问题。