Set up long running tasks by calling logic app workflows with Azure Functions

Applies to: Azure Logic Apps (Consumption)

When you need to deploy a long-running listener or task, you can create a logic app workflow that uses the Request trigger and Azure Functions to call that trigger and run the workflow.

For example, you can create a function that listens for messages that arrive in an Azure Service Bus queue. When this event happens, the function calls the Request trigger, which works as a push trigger to automatically run your workflow.

This how-to guide shows how to create a logic app workflow that starts with the Request trigger. You then create a function that listens to a Service Bus queue. When a message arrives in the queue, the function calls the endpoint created by the Request trigger to run your workflow.

Note

Although you can implement this behavior using either a Consumption or Standard logic app workflow, this example continues with a Consumption workflow.

Prerequisites

Create a logic app workflow

  1. In the Azure portal, create a Consumption blank logic app by selecting the Blank Logic App template.

  2. After the designer opens, under the designer search box, select Built-in. In the search box, enter request.

  3. From the triggers list, select the trigger named When a HTTP request is received.

    Screenshot of the designer in the portal. The search box contains 'http request.' Under 'Triggers,' 'When a HTTP request is received' is highlighted.

    With the Request trigger, you can optionally enter a JSON schema to use with the queue message. JSON schemas help the designer understand the structure for the input data, and make the outputs easier for you to use in your workflow.

  4. To specify a schema, enter the schema in the Request Body JSON Schema box.

    Screenshot of the details of an HTTP request trigger. Some JSON code is visible in the 'Request Body JSON Schema' box.

    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.

      Screenshot of the details of an HTTP request trigger. Under 'Enter or paste a sample JSON payload,' some payload data is visible.

      The sample payload that's pictured earlier generates the following schema, which 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"
                  }
               }
            }
         }
      }
      
  5. Under the trigger, add any other actions that you want to use to process the received message.

    For example, you can add an action that sends email with the Office 365 Outlook connector.

  6. Save your logic app workflow.

    This step generates the callback URL for the Request trigger in your workflow. 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.

    Screenshot of the details of an HTTP request trigger. Next to 'HTTP POST URL,' a URL is visible.

Create a function

Next, create the function that listens to the queue and calls the endpoint on the Request trigger when a message arrives.

  1. In the Azure portal, open your function app.

  2. On the function app navigation menu, select Functions. On the Functions pane, select Create.

    Screenshot of a function app with 'Functions' highlighted on the function app menu. The 'Functions' page is opened, and 'Create' is highlighted.

  3. Under Select a template, select the template named Azure Service Bus Queue trigger. After the Template details section appears, which shows different options based on your template selection, provide the following information:

    Property Value Description
    New Function <function-name> Enter a name for your function.
    Service Bus connection <Service-Bus-connection> Select New to set up the connection for your Service Bus queue, which uses the Service Bus SDK OnMessageReceive() listener.
    Queue name <queue-name> Enter the name for your queue.

    Screenshot of the 'Create function' pane with 'Azure Service Bus Queue trigger' highlighted, and template example details entered.

  4. When you're done, select Create.

    The Azure portal now shows the Overview page for your new Azure Service Bus Queue trigger function.

  5. Now, write a basic function to call the endpoint for the logic app workflow that you created earlier. Before you write your function, review the following considerations:

    The following example uses the Task.Run method in asynchronous mode. For more information, see Asynchronous programming with async and await. The example also uses the application/json message content type, but you can change this type as necessary.

    using System;
    using System.Threading.Tasks;
    using System.Net.Http;
    using System.Text;
    
    // Set up the URI for the logic app workflow. You can also get this value on the logic app's 'Overview' pane, under the trigger history, or from an environment variable.
    private static string logicAppUri = @"https://prod-05.chinanorth.logic.azure.cn:443/workflows/<remaining-callback-URL>";
    
    // Reuse the instance of HTTP clients if possible. For more information, see /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")); 
    }
    

Test your logic app workflow

For testing, add a message to your Service Bus queue by using the following steps or other tool:

  1. In the Azure portal, open your Service Bus namespace.

  2. On the Service Bus namespace navigation menu, select Queues.

    Screenshot of a Service Bus namespace. On the navigation menu, 'Queues' is highlighted.

  3. Select the Service Bus queue that you linked to your function earlier using a Service Bus connection.

  4. On the queue navigation menu, select Service Bus Explorer, and then on the toolbar, select Send messages.

    Screenshot of a Service Bus queue page in the portal, with 'Send messages' highlighted. On the navigation menu, 'Service Bus Explorer' is highlighted.

  5. On the Send messages pane, specify the message to send to your Service Bus queue.

    This message triggers your logic app workflow.

Next steps