您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

Azure 函数事件网格触发器本地调试Azure Function Event Grid Trigger Local Debugging

本文演示如何调试一个用于处理存储帐户所引发 Azure 事件网格事件的本地函数。This article demonstrates how to debug a local function that handles an Azure Event Grid event raised by a storage account.

必备组件Prerequisites

  • 创建或使用现有的函数应用Create or use an existing function app
  • 创建或使用现有的存储帐户Create or use an existing storage account
  • 下载 ngrok,使 Azure 能够调用本地函数Download ngrok to allow Azure to call your local function

创建新函数Create a new function

在 Visual Studio 中打开函数应用,在解决方案资源管理器中右键单击项目名称,然后单击“添加”>“新建 Azure 函数”。 Open your function app in Visual Studio and, right-click on the project name in the Solution Explorer and click Add > New Azure Function.

在“新建 Azure 函数”窗口中选择“事件网格触发器”,然后单击“确定”。 In the New Azure Function window, select Event Grid trigger and click OK.

创建新的函数

创建函数后,打开代码文件,并复制文件顶部已注释掉的 URL。Once the function is created, open the code file and copy the URL commented out at the top of the file. 配置事件网格触发器时将使用此位置。This location is used when configuring the Event Grid trigger.

复制位置

然后,在以 log.LogInformation 开头的行中设置一个断点。Then, set a breakpoint on the line that begins with log.LogInformation.

设置断点

接下来,按 F5 启动调试会话。Next, press F5 to start a debugging session.

使 Azure 能够调用本地函数Allow Azure to call your local function

若要在计算机上调试的函数处中断,必须通过某种方式使 Azure 能够在云中与本地函数通信。To break into a function being debugged on your machine, you must enable a way for Azure to communicate with your local function from the cloud.

ngrok 实用工具可让 Azure 调用计算机上运行的函数。The ngrok utility provides a way for Azure to call the function running on your machine. 使用以下命令启动 ngrokStart ngrok using the following command:

ngrok http -host-header=localhost 7071

设置该实用工具后,命令窗口应如以下屏幕截图所示:As the utility is set up, the command window should look similar to the following screenshot:

启动 ngrok

复制运行 ngrok 时生成的 HTTPS URL。Copy the HTTPS URL generated when ngrok is run. 配置事件网格事件终结点时将使用此值。This value is used when configuring the event grid event endpoint.

添加存储事件Add a storage event

打开 Azure 门户并导航到存储帐户,然后单击“事件”选项。 Open the Azure portal and navigate to a storage account and click on the Events option.

添加存储帐户事件

在“事件”窗口中,单击“事件订阅”按钮。 In the Events window, click on the Event Subscription button. 在“事件订阅”窗口中,单击“终结点类型”下拉列表并选择“Web Hook”。 In the Even Subscription window, click on the Endpoint Type dropdown and select Web Hook.

选择订阅类型

配置终结点类型后,单击“选择终结点”以配置终结点值。 Once the endpoint type is configured, click on Select an endpoint to configure the endpoint value.

选择终结点类型

“订阅者终结点”值由三个不同的值构成。 The Subscriber Endpoint value is made up from three different values. 前缀是 ngrok 生成的 HTTPS URL。The prefix is the HTTPS URL generated by ngrok. URL 的剩余部分来自函数代码文件中的 URL,其末尾添加了函数名称。The remainder of the URL comes from the URL found in the function code file, with the function name added at the end. 首先处理函数代码文件中的 URL:将 http://localhost:7071 替换为 ngrok URL,将 {functionname} 替换为函数名称。Starting with the URL from the function code file, the ngrok URL replaces http://localhost:7071 and the function name replaces {functionname}.

以下屏幕截图显示了最终 URL 的形式:The following screenshot shows how the final URL should look:

终结点选择

输入适当的值后,单击“确认选择”。 Once you've entered the appropriate value, click Confirm Selection.

重要

每次启动 ngrok 时,都会重新生成 HTTPS URL,并且值会更改。Every time you start ngrok, the HTTPS URL is regenerated and the value changes. 因此,每次通过 ngrok 向 Azure 公开函数时,都必须创建新的事件订阅。Therefore you must create a new Event Subscription each time you expose your function to Azure via ngrok.

上传文件Upload a file

现在,可将一个文件上传到存储帐户,以触发由本地函数处理的事件网格事件。Now you can upload a file to your storage account to trigger an Event Grid event for your local function to handle.

打开存储资源管理器并连接到存储帐户。Open Storage Explorer and connect to the your storage account.

  • 展开“Blob 容器” Expand Blob Containers
  • 单击右键并选择“创建 Blob 容器”。 Right-click and select Create Blob Container.
  • 将容器命名为 testName the container test
  • 选择 test 容器Select the test container
  • 单击“上传”按钮 Click the Upload button
  • 单击“上传文件” Click Upload Files
  • 选择文件并将其上传到 Blob 容器Select a file and upload it to the blob container

调试函数Debug the function

事件网格识别到已将新文件上传到存储容器之后,即会命中本地函数中的断点。Once the Event Grid recognizes a new file is uploaded to the storage container, the break point is hit in your local function.

启动 ngrok

清理资源Clean up resources

若要清理本文中创建的资源,请删除存储帐户中的 test 容器。To clean up the resources created in this article, delete the test container in your storage account.

后续步骤Next steps