适用于 Azure Functions 的 Azure 队列存储输出绑定Azure Queue storage output bindings for Azure Functions

Azure Functions 可以通过设置输出绑定来创建新的 Azure 队列存储消息。Azure Functions can create new Azure Queue storage messages by setting up an output binding.

有关设置和配置详细信息,请参阅概述For information on setup and configuration details, see the overview.

示例Example

以下示例演示针对收到的每个 HTTP 请求创建队列消息的C# 函数The following example shows a C# function that creates a queue message for each HTTP request received.

[StorageAccount("MyStorageConnectionAppSetting")]
public static class QueueFunctions
{
    [FunctionName("QueueOutput")]
    [return: Queue("myqueue-items")]
    public static string QueueOutput([HttpTrigger] dynamic input,  ILogger log)
    {
        log.LogInformation($"C# function processed: {input.Text}");
        return input.Text;
    }
}

特性和注释Attributes and annotations

C# 类库中,使用 QueueAttributeIn C# class libraries, use the QueueAttribute.

该特性将应用到 out 参数,或应用到函数的返回值。The attribute applies to an out parameter or the return value of the function. 该特性的构造函数采用队列的名称,如以下示例中所示:The attribute's constructor takes the name of the queue, as shown in the following example:

[FunctionName("QueueOutput")]
[return: Queue("myqueue-items")]
public static string Run([HttpTrigger] dynamic input,  ILogger log)
{
    ...
}

可以设置 Connection 属性来指定要使用的存储帐户,如以下示例中所示:You can set the Connection property to specify the storage account to use, as shown in the following example:

[FunctionName("QueueOutput")]
[return: Queue("myqueue-items", Connection = "StorageConnectionAppSetting")]
public static string Run([HttpTrigger] dynamic input,  ILogger log)
{
    ...
}

有关完整示例,请参阅输出示例For a complete example, see Output example.

可以使用 StorageAccount 特性在类、方法或参数级别指定存储帐户。You can use the StorageAccount attribute to specify the storage account at class, method, or parameter level. 有关详细信息,请参阅“触发器 - 特性”。For more information, see Trigger - attributes.

配置Configuration

下表解释了在 function.json 文件和 Queue 特性中设置的绑定配置属性。The following table explains the binding configuration properties that you set in the function.json file and the Queue attribute.

function.json 属性function.json property Attribute 属性Attribute property 说明Description
typetype 不适用n/a 必须设置为 queueMust be set to queue. 在 Azure 门户中创建触发器时,会自动设置此属性。This property is set automatically when you create the trigger in the Azure portal.
directiondirection 不适用n/a 必须设置为 outMust be set to out. 在 Azure 门户中创建触发器时,会自动设置此属性。This property is set automatically when you create the trigger in the Azure portal.
namename 不适用n/a 表示函数代码中的队列的变量的名称。The name of the variable that represents the queue in function code. 设置为 $return 可引用函数返回值。Set to $return to reference the function return value.
queueNamequeueName QueueNameQueueName 队列的名称。The name of the queue.
连接connection ConnectionConnection 包含要用于此绑定的存储连接字符串的应用设置的名称。The name of an app setting that contains the Storage connection string to use for this binding. 如果应用设置名称以“AzureWebJobs”开始,则只能在此处指定该名称的余下部分。If the app setting name begins with "AzureWebJobs", you can specify only the remainder of the name here.

例如,如果将 connection 设置为“MyStorage”,Functions 运行时将会查找名为“MyStorage”的应用设置。For example, if you set connection to "MyStorage", the Functions runtime looks for an app setting that is named "MyStorage." 如果将 connection 留空,函数运行时将使用名为 AzureWebJobsStorage 的应用设置中的默认存储连接字符串。If you leave connection empty, the Functions runtime uses the default Storage connection string in the app setting that is named AzureWebJobsStorage.

如果使用 5.x 版或更高版本的扩展,而不是使用连接字符串,则可以提供对用于定义连接的配置节的引用。If you are using version 5.x or higher of the extension, instead of a connection string, you can provide a reference to a configuration section which defines the connection. 请参阅连接See Connections.

在本地进行开发时,应用设置将取 local.settings.json 文件的值。When you're developing locally, app settings go into the local.settings.json file.

使用情况Usage

默认Default

使用 out T paramName 等方法参数写入一条队列消息。Write a single queue message by using a method parameter such as out T paramName. 可以使用方法返回类型而不使用 out 参数,T 可为以下任何类型:You can use the method return type instead of an out parameter, and T can be any of the following types:

如果在尝试绑定到 CloudQueueMessage 时出现错误消息,请确保引用正确的存储 SDK 版本If you try to bind to CloudQueueMessage and get an error message, make sure that you have a reference to the correct Storage SDK version.

在 C# 和 C# 脚本中,可使用以下类型之一编写多条队列消息:In C# and C# script, write multiple queue messages by using one of the following types:

其他类型Additional types

应用如果使用 5.0.0 版或更高版本的存储扩展,还可以使用用于 .NET 的 Azure SDK 中的类型。Apps using the 5.0.0 or higher version of the Storage extension may also use types from the Azure SDK for .NET. 此版本为了支持以下类型,删除了对旧的 CloudQueueCloudQueueMessage 类型的支持:This version drops support for the legacy CloudQueue and CloudQueueMessage types in favor of the following types:

有关使用这些类型的示例,请参阅扩展的 GitHub 存储库For examples using these types, see the GitHub repository for the extension.

异常和返回代码Exceptions and return codes

绑定Binding 参考Reference
队列Queue 队列错误代码Queue Error Codes
Blob、表、队列Blob, Table, Queue 存储错误代码Storage Error Codes
Blob、表、队列Blob, Table, Queue 故障排除Troubleshooting

后续步骤Next steps