Skip to content

Latest commit

 

History

History
149 lines (98 loc) · 7.72 KB

logic-apps-scenario-function-sb-trigger.md

File metadata and controls

149 lines (98 loc) · 7.72 KB
title description services ms.suite ms.reviewer ms.topic ms.date ms.custom
Call logic apps with Azure Functions
Create Azure functions that call or trigger logic apps by listening to Azure Service Bus
logic-apps
integration
jehollan, klam, logicappspm
article
11/08/2019
devx-track-csharp

Call or trigger logic apps by using Azure Functions and Azure Service Bus

You can use Azure Functions to trigger a logic app when you need to deploy a long-running listener or task. For example, you can create an Azure function that listens in on an Azure Service Bus queue and immediately fires 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. First, create a logic app that starts with an HTTP request trigger. The function calls that endpoint whenever a queue message is received.

  1. 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. In the search box, enter http request. From the triggers list, select the When a HTTP request is received trigger.

    Select trigger

    With the Request trigger, you can optionally enter a JSON schema to use with the queue message. 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. To specify a schema, enter the schema in the Request Body JSON Schema box, for example:

    Specify JSON schema

    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, select Use sample payload to generate schema.

    2. Under Enter or paste a sample JSON payload, enter your sample payload, and then select Done.

      Enter sample payload

    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.

    For example, you can send an email with the Office 365 Outlook connector.

  5. Save your logic app, which generates the callback URL for the trigger in this logic app. Later, you use this callback URL in the code for the Azure Service Bus Queue trigger.

    The callback URL appears in the HTTP POST URL property.

    Generated callback URL for trigger

Create Azure function

Next, create the function that acts as the trigger and listens to the queue.

  1. 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.

    Expand "Functions" and select "New function"

  3. 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

      Select template for new function app

    • For an existing function app, select this template: Service Bus Queue trigger - C#

      Select template for existing function app

  4. 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:

    This example uses the Task.Run method in asynchronous mode. 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.westus.logic.azure.com:443/workflows/<remaining-callback-URL>";
    
    // Reuse the instance of HTTP clients if possible: https://docs.microsoft.com/azure/azure-functions/manage-connections
    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