使用 Azure Functions 和 Azure 服务总线调用或触发逻辑应用Call or trigger logic apps by using Azure Functions and Azure Service Bus

需要部署长时间运行的侦听器或任务时,可以使用 Azure Functions 为逻辑应用创建触发器。You can use Azure Functions to create a trigger for a logic app when you need to deploy a long-running listener or task. 例如,可以创建一个函数以侦听队列,并立即以推送触发器的形式触发逻辑应用。For example, you can create a function that listens in on a queue and then immediately fire a logic app as a push trigger.

先决条件Prerequisites

创建逻辑应用Create logic app

在此方案中,你有一个函数,其运行的每个逻辑应用都是你要触发的。For this scenario, you have a function running each logic app that you want to trigger. 首先,创建一个通过 HTTP 请求触发器启动的逻辑应用。First, create a logic app that starts with an HTTP request trigger. 每当收到队列消息时,函数会调用该终结点。The function calls that endpoint whenever a queue message is received.

  1. 登录到 Azure 门户,并创建一个空的逻辑应用。Sign in to the Azure portal, and create blank logic app.

    如果不熟悉逻辑应用,请查看快速入门:创建第一个逻辑应用If you're new to logic apps, review Quickstart: Create your first logic app.

  2. 在搜索框中输入 http requestIn the search box, enter http request. 从触发器列表中选择“当收到 HTTP 请求时”触发器。 From the triggers list, select the When a HTTP request is received trigger.

    选择触发器

    使用请求触发器时,可以选择输入用于队列消息的 JSON 架构。With the Request trigger, you can optionally enter a JSON schema to use with the queue message. JSON 架构帮助逻辑应用设计器理解输入数据的结构,并方便你在工作流中使用输出。JSON schemas help the Logic App Designer understand the structure for the input data, and make the outputs easier for you to use in your workflow.

  3. 若要指定架构,请在“请求正文 JSON 架构” 框中输入架构,例如:To specify a schema, enter the schema in the Request Body JSON Schema box, for example:

    指定 JSON 架构

    如果你没有架构,但有一个 JSON 格式的示例有效负载,则可以基于该有效负载生成一个架构。If you don't have a schema, but you have a sample payload in JSON format, you can generate a schema from that payload.

    1. 在请求触发器中,选择“使用示例有效负载生成架构”。 In the Request trigger, choose Use sample payload to generate schema.

    2. 在“输入或粘贴示例 JSON 有效负载”下,输入你的示例有效负载,然后选择“完成”。 Under Enter or paste a sample JSON payload, enter your sample payload, and then choose Done.

      输入示例有效负载

    此示例有效负载生成在触发器中显示的以下架构:This sample payload generates this schema that appears in the trigger:

    {
       "type": "object",
       "properties": {
          "address": {
             "type": "object",
             "properties": {
                "number": {
                   "type": "integer"
                },
                "street": {
                   "type": "string"
                },
                "city": {
                   "type": "string"
                },
                "postalCode": {
                   "type": "integer"
                },
                "country": {
                   "type": "string"
                }
             }
          }
       }
    }
    
  4. 添加你希望在收到队列消息后运行的任何其他操作。Add any other actions that you want to run after receiving the queue message.

    例如,可以通过 Office 365 Outlook 连接器发送一封电子邮件。For example, you can send an email with the Office 365 Outlook connector.

  5. 保存你的逻辑应用,这将在此逻辑应用中生成触发器的回调 URL。Save your logic app, which generates the callback URL for the trigger in this logic app. 随后,在用于 Azure 服务总线队列触发器的代码中使用此回调 URL。Later, you use this callback URL in the code for the Azure Service Bus Queue trigger.

    此回调 URL 显示在 HTTP POST URL 属性中。The callback URL appears in the HTTP POST URL property.

    为触发器生成的回调 URL

创建 Azure 函数Create Azure function

接下来,创建一个充当触发器并侦听队列的函数。Next, create the function that acts as the trigger and listens to the queue.

  1. 在 Azure 门户中,打开并展开你的函数应用(如果尚未打开)。In the Azure portal, open and expand your function app, if not already open.

  2. 在你的函数应用名称下,展开“函数”。 Under your function app name, expand Functions. 在“函数”窗格中,选择“新建函数”。 On the Functions pane, select New function.

    展开“函数”,选择“新建函数”

  3. 根据你是创建新的函数应用(在其中选择 .NET 作为运行时堆栈)还是使用现有的函数应用来选择此模板。Select this template based on whether you created a new function app where you selected .NET as the runtime stack, or you're using an existing function app.

    • 对于新的函数应用,请选择此模板:服务总线队列触发器For new function apps, select this template: Service Bus Queue trigger

      为新的函数应用选择模板

    • 对于现有的函数应用,请选择此模板:服务总线队列触发器 - C#For an existing function app, select this template: Service Bus Queue trigger - C#

      为现有的函数应用选择模板

  4. 在“Azure 服务总线队列触发器”窗格中,为你的触发器提供一个名称,然后为队列设置使用 Azure 服务总线 SDK OnMessageReceive() 侦听器的“服务总线连接”,然后选择“创建”。 On the Azure Service Bus Queue trigger pane, provide a name for your trigger, and set up the Service Bus connection for the queue, which uses the Azure Service Bus SDK OnMessageReceive() listener, and select Create.

  5. 编写一个基本函数,用以通过将队列消息用作触发器来调用之前创建的逻辑应用终结点。Write a basic function to call the previously created logic app endpoint by using the queue message as a trigger. 在编写函数之前,请查看以下注意事项:Before you write your function, review these considerations:

    此示例以异步模式使用 Task.Run 方法This example uses the Task.Run method in asynchronous mode. 有关详细信息,请参阅使用 async 和 await 进行异步编程For more information, see Asynchronous programming with async and await.

    using System;
    using System.Threading.Tasks;
    using System.Net.Http;
    using System.Text;
    
    // Can also fetch from App Settings or environment variable
    private static string logicAppUri = @"https://prod-05.chinaeast.logic.azure.cn:443/workflows/<remaining-callback-URL>";
    
    // Reuse the instance of HTTP clients if possible
    private static HttpClient httpClient = new HttpClient();
    
    public static async Task Run(string myQueueItem, TraceWriter log) 
    {
       log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
       var response = await httpClient.PostAsync(logicAppUri, new StringContent(myQueueItem, Encoding.UTF8, "application/json")); 
    }
    
  6. 若要测试函数,请使用服务总线资源管理器之类的工具添加一个队列消息。To test the function, add a queue message by using a tool such as the Service Bus Explorer.

    逻辑应用在该函数收到消息之后立即触发。The logic app triggers immediately after the function receives the message.

后续步骤Next steps

使用 HTTP 终结点调用、触发或嵌套工作流Call, trigger, or nest workflows by using HTTP endpoints