Azure Functions 触发器和绑定示例Azure Functions trigger and binding example

本文展示了如何在 Azure 函数中配置触发器和绑定This article demonstrates how to configure a trigger and bindings in an Azure Function.

假设希望在 Azure 队列存储中显示一条新消息时就将一个新行写入 Azure 表存储。Suppose you want to write a new row to Azure Table storage whenever a new message appears in Azure Queue storage. 使用 Azure 队列存储触发器和 Azure 表存储输出绑定即可实现此方案。This scenario can be implemented using an Azure Queue storage trigger and an Azure Table storage output binding.

下面是用于这种方案的 function.json 文件。Here's a function.json file for this scenario.

{
  "bindings": [
    {
      "type": "queueTrigger",
      "direction": "in",
      "name": "order",
      "queueName": "myqueue-items",
      "connection": "MY_STORAGE_ACCT_APP_SETTING"
    },
    {
      "type": "table",
      "direction": "out",
      "name": "$return",
      "tableName": "outTable",
      "connection": "MY_TABLE_STORAGE_ACCT_APP_SETTING"
    }
  ]
}

bindings 数组中的第一个元素是队列存储触发器。The first element in the bindings array is the Queue storage trigger. typedirection 属性标识该触发器。The type and direction properties identify the trigger. name 属性标识接收队列消息内容的函数参数。The name property identifies the function parameter that receives the queue message content. 要监视的队列的名称位于 queueName 中,连接字符串位于由 connection 标识的应用设置中。The name of the queue to monitor is in queueName, and the connection string is in the app setting identified by connection.

bindings 数组中的第二个元素是 Azure 表存储输出绑定。The second element in the bindings array is the Azure Table Storage output binding. typedirection 属性标识该绑定。The type and direction properties identify the binding. name 属性指定函数提供新表行的方式,在此例中是使用函数返回值来提供。The name property specifies how the function provides the new table row, in this case by using the function return value. 表格的名称位于 tableName 中,连接字符串位于由 connection 标识的应用设置中。The name of the table is in tableName, and the connection string is in the app setting identified by connection.

若要在 Azure 门户中查看和编辑 function.json 的内容,请单击函数“集成”选项卡上的“高级编辑器”选项。To view and edit the contents of function.json in the Azure portal, click the Advanced editor option on the Integrate tab of your function.

Note

connection 的值是包含连接字符串的应用设置的名称,而不是连接字符串本身的名称。The value of connection is the name of an app setting that contains the connection string, not the connection string itself. 绑定使用应用设置中存储的连接字符串,以强制执行 function.json 不包含服务密钥这一最佳做法。Bindings use connection strings stored in app settings to enforce the best practice that function.json does not contain service secrets.

C# 脚本示例C# script example

以下是适用于此触发器和绑定的 C# 脚本代码。Here's C# script code that works with this trigger and binding. 请注意,提供队列消息内容的参数的名称是 order;需使用此名称是因为 function.json 中的 name 属性值是 orderNotice that the name of the parameter that provides the queue message content is order; this name is required because the name property value in function.json is order

#r "Newtonsoft.Json"

using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;

// From an incoming queue message that is a JSON object, add fields and write to Table storage
// The method return value creates a new row in Table Storage
public static Person Run(JObject order, ILogger log)
{
    return new Person() { 
            PartitionKey = "Orders", 
            RowKey = Guid.NewGuid().ToString(),  
            Name = order["Name"].ToString(),
            MobileNumber = order["MobileNumber"].ToString() };  
}
 
public class Person
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Name { get; set; }
    public string MobileNumber { get; set; }
}

JavaScript 示例JavaScript example

可将同一 function.json 文件用于 JavaScript 函数:The same function.json file can be used with a JavaScript function:

// From an incoming queue message that is a JSON object, add fields and write to Table Storage
// The second parameter to context.done is used as the value for the new row
module.exports = function (context, order) {
    order.PartitionKey = "Orders";
    order.RowKey = generateRandomId(); 

    context.done(null, order);
};

function generateRandomId() {
    return Math.random().toString(36).substring(2, 15) +
        Math.random().toString(36).substring(2, 15);
}

类库示例Class library example

在类库中,由特性而不是 function.json 文件提供这些触发器和绑定信息 — 队列和表名称、存储帐户、输入和输出 — 的函数参数。In a class library, the same trigger and binding information — queue and table names, storage accounts, function parameters for input and output — is provided by attributes instead of a function.json file. 下面是一个示例:Here's an example:

public static class QueueTriggerTableOutput
{
    [FunctionName("QueueTriggerTableOutput")]
    [return: Table("outTable", Connection = "MY_TABLE_STORAGE_ACCT_APP_SETTING")]
    public static Person Run(
        [QueueTrigger("myqueue-items", Connection = "MY_STORAGE_ACCT_APP_SETTING")]JObject order,
        ILogger log)
    {
        return new Person() {
                PartitionKey = "Orders",
                RowKey = Guid.NewGuid().ToString(),
                Name = order["Name"].ToString(),
                MobileNumber = order["MobileNumber"].ToString() };
    }
}

public class Person
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Name { get; set; }
    public string MobileNumber { get; set; }
}

你现在有一个由 Azure 表存储触发的工作函数,该函数将数据输出到队列中。You now have a working function that is triggered by Azure Table storage which outputs data to a queue.

后续步骤Next steps