Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HTTPTrigger函数中未找到SendGrid_C#_Azure_Azure Functions - Fatal编程技术网

C# HTTPTrigger函数中未找到SendGrid

C# HTTPTrigger函数中未找到SendGrid,c#,azure,azure-functions,C#,Azure,Azure Functions,我正在尝试创建一个Azure函数,该函数将由HTTP在macOS中使用VSCode触发 使用official,我在本地机器上成功地创建了一个HTTP触发的azure函数 现在我想在此功能期间发送一封电子邮件,但当我添加senGrid引用时,我遇到了一个编译错误: 以下1个功能出错: [15/01/2018 21:51:12]ResetPasswordSendEmail:C#编译服务错误:无法加载文件或程序集“SendGrid,Culture=neutral,PublicKeyToken=null

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

使用official,我在本地机器上成功地创建了一个HTTP触发的azure函数

现在我想在此功能期间发送一封电子邮件,但当我添加senGrid引用时,我遇到了一个编译错误:

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

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");
}
{
    "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"
        }
    ]
}
函数json:

#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");
}
{
    "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

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

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

azure功能:

#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");
}
{
  "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
}
函数json:

#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");
}
{
  "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
}

有关更多详细信息,您可以阅读本文以了解如何在Azure函数中使用sendgrid。

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

导航到项目根文件夹(其中
host.json
is)并执行以下命令:

func extensions install -p Microsoft.Azure.WebJobs.Extensions.SendGrid -v 3.0.0-beta4
这将把
SendGrid
二进制文件添加到函数应用程序的
bin
文件夹中

然后,您的脚本似乎是v1和v2函数的混合体。例如,
CreateResponse()
不属于
HttpRequest
并且
Mail
类不在
SendGrid
程序集中。下面是一个从HTTP函数发送邮件的非常简单的示例:

publicstaticiactionresult运行(HttpRequest请求、TraceWriter日志、out SendGridMessage消息)
{
Info(“C#HTTP触发器函数处理了一个请求。”);
message=新的SendGridMessage();
message.AddTo(“you@gmail.com");
message.AddContent(“text/html”、“测试体”);
message.SetFrom(新电子邮件地址(“test@test.com"));
message.SetSubject(“主题”);
返回新的OkObjectResult(“OK”);
}

我认为您需要参考NuGet软件包
Microsoft.Azure.WebJobs.Extensions.SendGrid
在哪里?我没有看到任何csproj和手动添加项目。json无法解决问题这是我的第一个azure函数。所以我肯定错过了很多信息,谢谢你的扩展安装。我正在尝试安装扩展并重建function@OrcusZ您是否在
bin
文件夹和
function extensions
文件夹中获得了
SendGrid.dll
?我将它安装在我的函数项目中,而不是函数文件夹中,它可以正常工作。谢谢:D