使用 Functions 将消息添加到 Azure 存储队列Add messages to an Azure Storage queue using Functions

在 Azure Functions 中,可以通过输入和输出绑定以声明方式将外部服务中的数据提供给代码使用。In Azure Functions, input and output bindings provide a declarative way to make data from external services available to your code. 在本快速入门中,可以使用输出绑定,这样便可以当某个 HTTP 请求触发某个函数时,在队列中创建一条消息。In this quickstart, you use an output binding to create a message in a queue when a function is triggered by an HTTP request. 可以使用 Azure 存储容器查看函数创建的队列消息。You use Azure storage container to view the queue messages that your function creates.

先决条件Prerequisites

完成本快速入门教程需要:To complete this quickstart:

添加输出绑定Add an output binding

在此部分,请使用门户 UI 将队列存储输出绑定添加到此前创建的函数。In this section, you use the portal UI to add a queue storage output binding to the function you created earlier. 有了此绑定,就可以在尽量减少代码编写工作的情况下在队列中创建消息。This binding makes it possible to write minimal code to create a message in a queue. 不需为打开存储连接、创建队列、获取队列引用之类的任务编写代码。You don't have to write code for tasks such as opening a storage connection, creating a queue, or getting a reference to a queue. Azure Functions 运行时和队列输出绑定为你处理这些任务。The Azure Functions runtime and queue output binding take care of those tasks for you.

  1. 在 Azure 门户中,打开在通过 Azure 门户创建第一个函数中创建的函数应用的函数应用页。In the Azure portal, open the function app page for the function app that you created in Create your first function from the Azure portal. 若要打开该页面,请搜索并选择“函数应用”。To do open the page, search for and select Function App. 然后,选择你的函数应用。Then, select your function app.

  2. 选择你的函数应用,然后选择在前面的快速入门中创建的函数。Select your function app, and then select the function that you created in that earlier quickstart.

  3. 选择“集成”,然后选择“+ 添加输出”。Select Integration, and then select + Add output.

    为函数创建输出绑定。

  4. 选择“Azure 队列存储”绑定类型,并添加在此屏幕截图下的表中指定的设置:Select the Azure Queue Storage binding type, and add the settings as specified in the table that follows this screenshot:

    为函数创建输出绑定。

    设置Setting 建议的值Suggested value 说明Description
    消息参数名称Message parameter name outputQueueItemoutputQueueItem 输出绑定参数的名称。The name of the output binding parameter.
    队列名称Queue name outqueueoutqueue 要连接到存储帐户中的队列的名称。The name of the queue to connect to in your Storage account.
    存储帐户连接Storage account connection AzureWebJobsStorageAzureWebJobsStorage 可以使用 Function App 已在使用的存储帐户连接,也可以创建一个新的存储帐户连接。You can use the storage account connection already being used by your function app, or create a new one.
  5. 选择“确定”以添加绑定。Select OK to add the binding.

现在,已定义输出绑定,需要更新代码以使用绑定将消息添加到队列。Now that you have an output binding defined, you need to update the code to use the binding to add messages to a queue.

添加使用输出绑定的代码Add code that uses the output binding

在此部分,请添加将消息写入输出队列的代码。In this section, you add code that writes a message to the output queue. 该消息包括在查询字符串中传递到 HTTP 触发器的值。The message includes the value that is passed to the HTTP trigger in the query string. 例如,如果查询字符串包含 name=Azure,则队列消息将是“传递给函数的名称:Azure”。For example, if the query string includes name=Azure, the queue message will be Name passed to the function: Azure.

  1. 在函数中,选择“代码 + 测试”以在编辑器中显示函数代码。In your function, select Code + Test to display the function code in the editor.

  2. 根据函数语言更新函数代码:Update the function code depending on your function language:

    向方法签名添加 outputQueueItem 参数,如以下示例所示。Add an outputQueueItem parameter to the method signature as shown in the following example.

    public static async Task<IActionResult> Run(HttpRequest req,
        ICollector<string> outputQueueItem, ILogger log)
    {
        ...
    }
    

    return 语句之前的函数体中,添加使用参数创建队列消息的代码。In the body of the function just before the return statement, add code that uses the parameter to create a queue message.

    outputQueueItem.Add("Name passed to the function: " + name);
    
  3. 选择“保存”以保存更改。Select Save to save changes.

测试函数Test the function

  1. 保存代码更改后,选择“测试”。After the code changes are saved, select Test.

  2. 确认你的测试与下图一致,然后选择“运行”。Confirm that your test matches the image below and select Run.

    为函数创建输出绑定。

    请注意,请求正文包含 nameAzureNotice that the Request body contains the name value Azure. 此值显示在队列消息中,该消息是在调用函数时创建的。This value appears in the queue message that is created when the function is invoked.

    如果不想选择此处的“运行”,也可调用该函数,方法是在浏览器中输入 URL,然后在查询字符串中指定 name 值。As an alternative to selecting Run here, you can call the function by entering a URL in a browser and specifying the name value in the query string. 此浏览器方法在以前的快速入门中演示过。The browser method is shown in the previous quickstart.

  3. 检查日志以确保该函数成功。Check the logs to make sure that the function succeeded.

首次使用输出绑定时,Functions 运行时会在存储帐户中创建名为 outqueue 的新队列。A new queue named outqueue is created in your Storage account by the Functions runtime when the output binding is first used. 可以使用存储帐户来验证是否已创建队列以及其中的消息。You'll use storage account to verify that the queue and a message in it were created.

查找连接到 AzureWebJobsStorage 的存储帐户Find the storage account connected to AzureWebJobsStorage

  1. 转到函数应用,选择“配置”。Go to your function app and select Configuration.

  2. 在“应用程序设置”下,选择“AzureWebJobsStorage”。Under Application settings, select AzureWebJobsStorage.

    为函数创建输出绑定。

  3. 查找并记下帐户名。Locate and make note of the account name.

    为函数创建输出绑定。

检查输出队列Examine the output queue

  1. 在函数应用的资源组中,选择用于本快速入门的存储帐户。In the resource group for your function app, select the storage account that you're using for this quickstart.

  2. 在“队列服务”下,选择“队列”,然后选择名为 outqueue 的队列。Under Queue service, select Queues and select the queue named outqueue.

    此队列包含在运行 HTTP 触发的函数时队列输出绑定创建的消息。The queue contains the message that the queue output binding created when you ran the HTTP-triggered function. 如果使用 Azure 的默认 name 值调用了此函数,则队列消息为“传递给函数的名称: Azure”。If you invoked the function with the default name value of Azure, the queue message is Name passed to the function: Azure.

  3. 再次运行函数,此时会看到新消息出现在队列中。Run the function again, and you'll see a new message appear in the queue.

清理资源Clean up resources

本教程系列中的其他快速入门教程是在本文的基础上制作的。Other quickstarts in this collection build upon this quickstart. 如果打算使用后续的快速入门、教程或者在本快速入门中创建的任何服务,请勿清理这些资源。If you plan to work with subsequent quickstarts, tutorials, or with any of the services you have created in this quickstart, do not clean up the resources.

Azure 中的资源是指函数应用、函数、存储帐户等。Resources in Azure refer to function apps, functions, storage accounts, and so forth. 这些资源可以组合到资源组 中,删除该组即可删除组中的所有内容。They're grouped into resource groups, and you can delete everything in a group by deleting the group.

已创建完成这些快速入门所需的资源。You created resources to complete these quickstarts. 这些资源可能需要付费,具体取决于帐户状态服务定价You may be billed for these resources, depending on your account status and service pricing. 如果不再需要这些资源,请参阅下面介绍的资源删除方法:If you don't need the resources anymore, here's how to delete them:

  1. 在 Azure 门户中转到“资源组”页。 In the Azure portal, go to the Resource group page.

    若要从函数应用页转到该页,请选择“概览”选项卡,然后选择“资源组”下的链接。 To get to that page from the function app page, select the Overview tab and then select the link under Resource group.

    从函数应用页选择要删除的资源组。

    若要从仪表板转到该页,请选择“资源组”,然后选择用于本快速入门的资源组。 To get to that page from the dashboard, select Resource groups, and then select the resource group that you used for this quickstart.

  2. 在“资源组”页中查看所包括的资源的列表,然后验证这些资源是否是要删除的。 In the Resource group page, review the list of included resources, and verify that they're the ones you want to delete.

  3. 选择“删除资源组” ,然后按说明操作。Select Delete resource group, and follow the instructions.

    可能需要数分钟才能删除完毕。Deletion may take a couple of minutes. 完成后会显示一个通知,持续数秒。When it's done, a notification appears for a few seconds. 也可以选择页面顶部的钟形图标来查看通知。You can also select the bell icon at the top of the page to view the notification.

后续步骤Next steps

本快速入门介绍了如何将输出绑定添加到现有函数。In this quickstart, you added an output binding to an existing function. 有关队列存储绑定的详细信息,请参阅 Azure Functions 存储队列绑定For more information about binding to Queue storage, see Azure Functions Storage queue bindings.