适用于 Azure Functions 的 Azure 队列存储触发器和绑定概述Azure Queue storage trigger and bindings for Azure Functions overview

Azure Functions 可以在创建新的 Azure 队列存储消息时运行,并可在函数中写入队列消息。Azure Functions can run as new Azure Queue storage messages are created and can write queue messages within a function.

操作Action 类型Type
在队列存储数据更改时运行函数Run a function as queue storage data changes 触发器Trigger
写入队列存储消息Write queue storage messages 输出绑定Output binding

添加到 Functions 应用Add to your Functions app

Functions 2.x 及更高版本Functions 2.x and higher

使用触发器和绑定需要引用相应的包。Working with the trigger and bindings requires that you reference the appropriate package. NuGet 包用于 .NET 类库,而扩展捆绑包用于其他所有应用程序类型。The NuGet package is used for .NET class libraries while the extension bundle is used for all other application types.

语言Language 添加方式...Add by... 备注Remarks
C#C# 安装 NuGet 包版本 3.xInstalling the NuGet package, version 3.x
C# 脚本、Java、JavaScript、PowerShellC# Script, Java, JavaScript, PowerShell 注册扩展捆绑包Registering the extension bundle 建议将 Azure Tools 扩展用于 Visual Studio Code。The Azure Tools extension is recommended to use with Visual Studio Code.
C# 脚本(Azure 门户中仅限联机)C# Script (online-only in Azure portal) 添加绑定Adding a binding 若要更新现有绑定扩展而不必重新发布函数应用,请参阅更新扩展To update existing binding extensions without having to republish your function app, see Update your extensions.

存储扩展 5.x 和更高版本Storage extension 5.x and higher

新版本的存储绑定扩展可用作预览 NuGet 包A new version of the Storage bindings extension is available as a preview NuGet package. 此预览版引入了使用标识而不是机密进行连接的功能。This preview introduces the ability to connect using an identity instead of a secret. 对于 .NET 应用程序,它还会更改可以绑定到的类型,并将 WindowsAzure.StorageMicrosoft.Azure.Storage 中的类型替换为 Azure.Storage.Queues 中的新类型。For .NET applications, it also changes the types that you can bind to, replacing the types from WindowsAzure.Storage and Microsoft.Azure.Storage with newer types from Azure.Storage.Queues.

备注

预览包不包括在扩展捆绑包中,必须手动安装。The preview package is not included in an extension bundle and must be installed manually. 对于 .NET 应用,请添加对包的引用。For .NET apps, add a reference to the package. 对于所有其他应用类型,请参阅更新扩展For all other app types, see Update your extensions.

Functions 1.xFunctions 1.x

Functions 1.x 应用会自动引用 Microsoft.Azure.WebJobs NuGet 程序包(版本 2.x)。Functions 1.x apps automatically have a reference the Microsoft.Azure.WebJobs NuGet package, version 2.x.

Functions 1.x 中的 Azure 存储 SDK 版本Azure Storage SDK version in Functions 1.x

在 Functions 1.x 中,存储触发器和绑定使用 7.2.1 版的 Azure 存储 SDK(WindowsAzure.Storage NuGet 包)。In Functions 1.x, the Storage triggers and bindings use version 7.2.1 of the Azure Storage SDK (WindowsAzure.Storage NuGet package). 如果引用另一版本的存储 SDK,而且在函数签名中绑定到某个存储 SDK 类型,则 Functions 运行时可能会报告它不能绑定到该类型。If you reference a different version of the Storage SDK, and you bind to a Storage SDK type in your function signature, the Functions runtime may report that it can't bind to that type. 此解决方案是为了确保项目引用 WindowsAzure.Storage 7.2.1The solution is to make sure your project references WindowsAzure.Storage 7.2.1.

host.json 设置host.json settings

本部分介绍版本 2.x 及更高版本中可用于此绑定的全局配置设置。This section describes the global configuration settings available for this binding in versions 2.x and higher. 下面的示例 host.json 文件仅包含此绑定的 2.x 版及更高版本设置。The example host.json file below contains only the version 2.x+ settings for this binding. 若要详细了解 2.x 版及更高版本中的全局配置设置,请参阅 Azure Functions 的 host.json 参考For more information about global configuration settings in versions 2.x and beyond, see host.json reference for Azure Functions.

备注

有关 Functions 1.x 中 host.json 的参考,请参阅 Azure Functions 1.x 的 host.json 参考For a reference of host.json in Functions 1.x, see host.json reference for Azure Functions 1.x.

{
    "version": "2.0",
    "extensions": {
        "queues": {
            "maxPollingInterval": "00:00:02",
            "visibilityTimeout" : "00:00:30",
            "batchSize": 16,
            "maxDequeueCount": 5,
            "newBatchThreshold": 8,
            "messageEncoding": "base64"
        }
    }
}
属性Property 默认Default 说明Description
maxPollingIntervalmaxPollingInterval 00:00:0100:00:01 队列轮询的最大间隔时间。The maximum interval between queue polls. 最小值为 00:00:00.100(100 毫秒),可递增至 00:01:00(1 分钟)。Minimum is 00:00:00.100 (100 ms) and increments up to 00:01:00 (1 min). 在函数 2.x 和更高版本中,数据类型是 TimeSpan,而在版本 1.x 中,它以毫秒为单位。In Functions 2.x and higher the data type is a TimeSpan, while in version 1.x it is in milliseconds.
visibilityTimeoutvisibilityTimeout 00:00:0000:00:00 消息处理失败时的重试间隔时间。The time interval between retries when processing of a message fails.
batchSizebatchSize 1616 Functions 运行时同时检索并并行处理的队列消息数。The number of queue messages that the Functions runtime retrieves simultaneously and processes in parallel. 当处理的数量下降到 newBatchThreshold 时,运行时可获取另一个批,并开始处理这些消息。When the number being processed gets down to the newBatchThreshold, the runtime gets another batch and starts processing those messages. 因此,每个函数处理的最大并发消息数是 batchSize 加上 newBatchThresholdSo the maximum number of concurrent messages being processed per function is batchSize plus newBatchThreshold. 此限制分别应用于各个队列触发的函数。This limit applies separately to each queue-triggered function.

如果要避免对队列上收到的消息并行执行,可以将 batchSize 设置为 1。If you want to avoid parallel execution for messages received on one queue, you can set batchSize to 1. 但是,只有在函数应用仅于单个虚拟机 (VM) 上运行时,此设置才可消除并发。However, this setting eliminates concurrency as long as your function app runs only on a single virtual machine (VM). 如果函数应用横向扩展到多个 VM,每个 VM 可运行每个队列触发的函数的一个实例。If the function app scales out to multiple VMs, each VM could run one instance of each queue-triggered function.

batchSize 的最大值为 32。The maximum batchSize is 32.
maxDequeueCountmaxDequeueCount 55 在将某个消息移到有害队列之前,尝试处理该消息的次数。The number of times to try processing a message before moving it to the poison queue.
newBatchThresholdnewBatchThreshold batchSize/2batchSize/2 只要同时处理的消息数下降到此数值,运行时即检索另一个批次。Whenever the number of messages being processed concurrently gets down to this number, the runtime retrieves another batch.
messageEncodingmessageEncoding base64base64 此设置仅适用于扩展版本 5.0.0 和更高版本This setting is only available in extension version 5.0.0 and higher. 它表示消息的编码格式。It represents the encoding format for messages. 有效值为 base64noneValid values are base64 and none.

后续步骤Next steps