触发SendGrid消息(Azure函数)当存储表被添加到



我有一个Azure函数,可以成功地排队存储表记录,正如我在Azure存储资源管理器中看到的。

我有一个通过SendGrid成功发送消息的Azure函数,并且应该在上述队列添加时触发,但它没有。

我猜有一个配置问题。它们都在同一个Function应用程序中,使用相同的连接字符串,但是SendGrid函数没有触发。这两个功能本身都是普通的,除了我的电子邮件地址更改。是否有其他配置可以使用?

HttpPost (CRUD) -CSharp1

{
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ],
      "authLevel": "function"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "table",
      "name": "outTable",
      "tableName": "orders",
      "connection": "matching_connection_string",
      "direction": "out"
    }
  ],
  "disabled": false
}

SendGridCSharp1

{
  "bindings": [
    {
      "type": "sendGrid",
      "name": "message",
      "direction": "out",
      "subject": "",
      "text": ""
    },
    {
      "type": "queueTrigger",
      "name": "order",
      "queueName": "orders",
      "connection": "matching_connection_string",
      "direction": "in"
    }
  ],
  "disabled": false
}

根据@DAXaholic的答案更新

DAXaholic建议我写一个表,但从队列触发。我更新了我的出站配置(将最后一个绑定类型更改为queue),并且我收到一个关于不知道使用哪个构造函数的错误:Function ($HttpPOST(CRUD)-CSharp1) Error: Can't figure out which ctor to call.我尝试将名称空间更改为Microsoft.WindowsAzure.Storage.Queue,但这似乎没有效果。

下面的代码:

#r "Microsoft.WindowsAzure.Storage"
using System.Net;
using Microsoft.WindowsAzure.Storage.Table;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ICollector<Order> outTable, TraceWriter log)
{
    var formData = await req.Content.ReadAsFormDataAsync();
    if(string.IsNullOrWhiteSpace(formData.Get("Name")))
    {
        return req.CreateResponse(HttpStatusCode.BadRequest, new {
           error = "no 'Name' property" 
        });
    }
    string name = formData["Name"].ToString();
    outTable.Add(new Order()
    {
        PartitionKey = "Functions",
        RowKey = Guid.NewGuid().ToString(),
        OrderId = string.Format("order{0}", name),
        CustomerName = name,
        CustomerEmail = string.Format("{0}@{1}.com", name, name)
    });
    return req.CreateResponse(HttpStatusCode.Created);
}
public class Order : TableEntity
{
    public string OrderId { get; set; }
    public string CustomerName { get; set; }
    public string CustomerEmail { get; set; }
}

我猜问题是你的触发器是基于Azure存储队列,但你正在写一个Azure存储表,所以前者没有触发。也就是说,尝试将到Azure Storage Table的出站绑定替换为到适当队列的绑定,如下所示(参见此处作为参考)

{
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ],
      "authLevel": "function"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "queue",
      "name": "outTable",
      "queueName": "orders",
      "connection": "matching_connection_string",
      "direction": "out"
    }
  ],
  "disabled": false
}

将出站代码更新为:

#r "Microsoft.WindowsAzure.Storage"
using System.Net;
using Microsoft.WindowsAzure.Storage.Queue;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ICollector<Order> outQueue, TraceWriter log)
{
    var formData = await req.Content.ReadAsFormDataAsync();
    if(string.IsNullOrWhiteSpace(formData.Get("Name")))
    {
        return req.CreateResponse(HttpStatusCode.BadRequest, new {
           error = "no 'Name' property" 
        });
    }
    string name = formData["Name"].ToString();
    outQueue.Add(new Order()
    {
        OrderId = string.Format("order{0}", name),
        CustomerName = name,
        CustomerEmail = string.Format("{0}@{1}.com", name, name)
    });
    return req.CreateResponse(HttpStatusCode.Created);
}
public class Order
{
    public string OrderId { get; set; }
    public string CustomerName { get; set; }
    public string CustomerEmail { get; set; }
}

最新更新