Search code examples
c#azureazure-functionscsx

Can't use Nuget package in C# Azure function


I'm trying to write an azure function which will use Sendgrid to send emails. However, I can't get my function to recognize the external nuget package. Here's what I have:

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("[email protected]", "xxx");
    var toAddr = new EmailAddress("xxxx", "xxx);
    var msg = MailHelper.CreateSingleEmail(fromAddr, toAddr, "subject", "content", "content");
    client.SendEmailAsync(msg).Wait();
}

I get this error:

[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?)

What am I missing?


Solution

  • If you are indeed on the v1 runtime then you're simply missing a using statement that has the EmailAddress type.

    Add this in —

    using SendGrid.Helpers.Mail;
    

    If you're on v2 (beta/.NET Core), just follow kim's URL from comments (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>
    

    The SendGrid NuGet package targets .NET Standard 1.3, so running on .NET Core should pose no problem.