将 Storage Table 添加到时触发 SendGrid 消息(Azure Functions)

Trigger SendGrid message (Azure Functions) when Storage Table is added to

我有一个 Azure 功能,它成功地将存储 table 记录排队,正如我在 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 建议我正在写入 table 但从队列中触发。我更新了我的出站配置(将最后一个绑定类型更改为 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 存储 Table,因此未触发前者。也就是说,尝试将到 Azure 存储 Table 的出站绑定替换为像这样的适当队列的绑定(参见 here 作为参考)

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