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 |
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.
-
An Azure subscription. If you don't have an Azure subscription, sign up for a free Azure account.
-
An Azure Service Bus namespace. If you don't have a namespace, create your namespace first.
-
An Azure function app, which is a container for Azure functions. If you don't have a function app, create your function app first, and make sure that you select .NET as the runtime stack.
-
Basic knowledge about how to create logic apps
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.
-
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.
-
In the search box, enter
http request
. From the triggers list, select the When a HTTP request is received 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.
-
To specify a schema, enter the schema in the Request Body JSON Schema box, for example:
If you don't have a schema, but you have a sample payload in JSON format, you can generate a schema from that payload.
-
In the Request trigger, select Use sample payload to generate schema.
-
Under Enter or paste a sample JSON payload, enter your sample payload, and then select 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" } } } } }
-
-
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.
-
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.
Next, create the function that acts as the trigger and listens to the queue.
-
In the Azure portal, open and expand your function app, if not already open.
-
Under your function app name, expand Functions. On the Functions pane, select New function.
-
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.
-
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. -
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
application/json
message content type, but you can change this type as necessary. -
Due to possible concurrently running functions, high volumes, or heavy loads, avoid instantiating the HTTPClient class with the
using
statement and directly creating HTTPClient instances per request. For more information, see Use HttpClientFactory to implement resilient HTTP requests. -
If possible, reuse the instance of HTTP clients. For more information, see Manage connections in Azure Functions.
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")); }
-
-
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.