将存储表添加到

时间:2016-09-13 05:57:19

标签: .net azure sendgrid azure-webjobs azure-functions

我有一个Azure功能,可以成功排队存储表记录,就像我在Azure存储资源管理器中看到的那样。

我有一个Azure功能,它通过SendGrid成功发送消息,而应该在上述队列添加时触发,但事实并非如此。

我猜是有配置问题。它们都在同一个Function应用程序中并使用相同的连接字符串,但SendGrid函数不会触发。这两个函数本身都是vanilla减去我的电子邮件地址更改。是否有其他配置可供使用?

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

1 个答案:

答案 0 :(得分:3)

我想问题是您的触发器基于Azure存储队列,但您正在写入Azure存储表,因此不会触发前者。也就是说,尝试使用绑定到适当的队列来替换到Azure存储表的出站绑定(请参阅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; }
}