Skip to content

Commit 6c3a69c

Browse files
authored
Merge pull request #3 from ecfan/patch-1
Peer review revisions
2 parents d21cd5a + 0b59c8a commit 6c3a69c

9 files changed

+75
-60
lines changed

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

Lines changed: 75 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
11
---
2-
title: Call logic app workflows with Azure Functions
3-
description: Trigger a logic app workflow whenever an Azure Service Bus message arrives in a queue by using Azure Functions to call an endpoint on the logic app workflow.
2+
title: Set up long-running tasks by calling workflows with Azure Functions
3+
description: Set up long-running tasks by creating an Azure Logic Apps workflow that monitors and responds to messages or events and uses Azure Functions to trigger the workflow.
44
services: logic-apps
55
ms.suite: integration
66
ms.reviewer: estfan, azla
77
ms.topic: how-to
88
ms.date: 10/31/2022
99
ms.custom: devx-track-csharp
10-
#Customer intent: As a logic apps developer, I want to use Azure Logic Apps to respond automatically and instantaneously to Azure Service Bus messages so that I don't have to monitor the messages manually.
10+
#Customer intent: As a logic apps developer, I want to set up a long-running task by creating a logic app workflow that monitors and responds to messages or events and uses Azure Functions to call the workflow.
1111
---
1212

13-
# Call or trigger logic app workflows by using Azure Functions and Azure Service Bus
13+
# Set up long running tasks by calling logic app workflows with Azure Functions
1414

1515
[!INCLUDE [logic-apps-sku-consumption](../../includes/logic-apps-sku-consumption.md)]
1616

17-
You can use [Azure Functions](../azure-functions/functions-overview.md) to trigger a logic app workflow when you need to deploy a long-running listener or task. For example, you can create a function that listens to an [Azure Service Bus](../service-bus-messaging/service-bus-messaging-overview.md) queue and immediately fires a logic app workflow as a push trigger.
17+
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.
1818

19-
This how-to guide shows you how to create a logic app workflow that starts with an HTTP request trigger. You also see how to create a function that listens to a message queue and calls the logic app endpoint whenever a queue message arrives.
19+
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.
20+
21+
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.
22+
23+
> [!NOTE]
24+
>
25+
> Although you can implement this behavior using either a Consumption and Standard logic app workflow,
26+
> this example continues with a Consumption workflow.
2027
2128
## Prerequisites
2229

2330
* An Azure account and subscription. If you don't have a subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
2431

25-
* A Service Bus namespace. If you don't have a namespace, [create your namespace first](../service-bus-messaging/service-bus-create-namespace-portal.md).
32+
* A Service Bus namespace. If you don't have a namespace, [create your namespace first](../service-bus-messaging/service-bus-create-namespace-portal.md). For more information, see [What is Azure Service Bus?](../service-bus-messaging/service-bus-messaging-overview.md)
2633

27-
* A function app, which is a container for your functions. If you don't have a function app, [create your function app first](../azure-functions/functions-get-started.md), and make sure that you select .NET as the runtime stack.
34+
* A function app, which is a container for your functions. If you don't have a function app, [create your function app first](../azure-functions/functions-get-started.md), and make sure to and make sure that you select .NET for the **Runtime stack** property.
2835

2936
* Basic knowledge about [how to create a Consumption logic app workflow](quickstart-create-first-logic-app-workflow.md).
3037

3138
## Create a logic app workflow
3239

33-
For this scenario, you have a function running for each logic app workflow that you want to trigger. First, you create a logic app workflow that starts with an HTTP request trigger. The function calls that endpoint whenever a queue message is received.
40+
1. In the [Azure portal](https://portal.azure.com), create a Consumption blank logic app by selecting the **Blank Logic App** template.
3441

35-
1. Sign in to the [Azure portal](https://portal.azure.com), and create a blank logic app workflow.
42+
1. After the designer opens, under the designer search box, select **Built-in**. In the search box, enter **request**.
3643

37-
If you're new to logic apps, review [Quickstart: Create your first logic app workflow](quickstart-create-first-logic-app-workflow.md).
38-
39-
1. In the search box, enter **http request**. From the triggers list, select the **When a HTTP request is received** trigger.
44+
1. From the triggers list, select the trigger named **When a HTTP request is received**.
4045

4146
:::image type="content" source="./media/logic-apps-scenario-function-sb-trigger/when-http-request-received-trigger.png" alt-text="Screenshot of the designer in the portal. The search box contains 'http request.' Under 'Triggers,' 'When a HTTP request is received' is highlighted.":::
4247

@@ -54,79 +59,87 @@ For this scenario, you have a function running for each logic app workflow that
5459

5560
:::image type="content" source="./media/logic-apps-scenario-function-sb-trigger/enter-sample-payload.png" alt-text="Screenshot of the details of an HTTP request trigger. Under 'Enter or paste a sample JSON payload,' some payload data is visible.":::
5661

57-
The sample payload that's pictured earlier generates the following schema, which appears in the trigger:
58-
59-
```json
60-
{
61-
"type": "object",
62-
"properties": {
63-
"address": {
64-
"type": "object",
65-
"properties": {
66-
"number": {
67-
"type": "integer"
68-
},
69-
"street": {
70-
"type": "string"
71-
},
72-
"city": {
73-
"type": "string"
74-
},
75-
"postalCode": {
76-
"type": "integer"
77-
},
78-
"country": {
79-
"type": "string"
62+
The sample payload that's pictured earlier generates the following schema, which appears in the trigger:
63+
64+
```json
65+
{
66+
"type": "object",
67+
"properties": {
68+
"address": {
69+
"type": "object",
70+
"properties": {
71+
"number": {
72+
"type": "integer"
73+
},
74+
"street": {
75+
"type": "string"
76+
},
77+
"city": {
78+
"type": "string"
79+
},
80+
"postalCode": {
81+
"type": "integer"
82+
},
83+
"country": {
84+
"type": "string"
85+
}
8086
}
8187
}
8288
}
8389
}
84-
}
85-
```
90+
```
8691

87-
1. Add any other actions that you want to run after receiving the queue message.
92+
1. Under the trigger, add any other actions that you want use to process the received message.
8893

89-
For example, you can send an email with the Office 365 Outlook connector.
94+
For example, you can add an action that sends email with the Office 365 Outlook connector.
9095

91-
1. Save your logic app workflow. This action generates the callback URL for the trigger in your logic app workflow. Later, you use this callback URL in the code for the Azure Service Bus Queue trigger.
96+
1. Save your logic app workflow.
9297

93-
The callback URL appears in the **HTTP POST URL** property.
98+
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.
9499

95100
:::image type="content" source="./media/logic-apps-scenario-function-sb-trigger/callback-URL-for-trigger.png" alt-text="Screenshot of the details of an HTTP request trigger. Next to 'HTTP POST URL,' a URL is visible.":::
96101

97102
## Create a function
98103

99-
Next, create the function that acts as the trigger and listens to the queue.
104+
Next, create the function that listens to the queue and calls the endpoint on the Request trigger when a message arrives.
100105

101-
1. In the Azure portal, go to your function app.
106+
1. In the [Azure portal](https://portal.azure.com), open your function app.
102107

103108
1. On the function app navigation menu, select **Functions**. On the **Functions** pane, select **Create**.
104109

105-
:::image type="content" source="./media/logic-apps-scenario-function-sb-trigger/add-new-function-to-function-app.png" alt-text="Screenshot of the 'Functions' page of a function app in the portal, with 'Create' highlighted. On the navigation menu, 'Functions' is highlighted.":::
110+
:::image type="content" source="./media/logic-apps-scenario-function-sb-trigger/add-new-function-to-function-app.png" alt-text="Screenshot of the Azure portal with a function app open, and 'Functions' is highlighted on the function app menu. The 'Functions' page is opened, and 'Create' is highlighted.":::
106111

107-
1. Select the **Azure Service Bus Queue trigger** template.
112+
1. 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:
108113

109-
:::image type="content" source="./media/logic-apps-scenario-function-sb-trigger/current-add-queue-trigger-template.png" alt-text="Screenshot of the 'Create function' page of a function app in the portal. Under 'Template,' 'Azure Service Bus Queue trigger' is highlighted.":::
114+
| Property | Value | Description |
115+
|----------|-------|-------------|
116+
| **New Function** | <*function-name*> | Enter a name for your function. |
117+
| **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. |
118+
| **Queue name** | <*queue-name*> | Enter the name for your queue. |
110119

111-
1. Under **Template details**, enter a name for your function, and set up the **Service Bus connection** for the queue, which uses the Service Bus SDK `OnMessageReceive()` listener. Next, enter the queue name, and then select **Create**.
120+
:::image type="content" source="./media/logic-apps-scenario-function-sb-trigger/current-add-queue-trigger-template.png" alt-text="Screenshot of the 'Create function' pane with 'Azure Service Bus Queue trigger' highlighted, and template example details entered.":::
112121

113-
1. Write a basic function to call the logic app endpoint that you created earlier. Use the queue message to trigger the function. Before you write your function, review the following considerations:
122+
1. When you're done, select **Create**.
123+
124+
The Azure portal now shows the **Overview** page for your new Azure Service Bus Queue trigger function.
125+
126+
1. 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:
127+
128+
* Trigger the function by using the message from the queue message.
114129

115-
* The following example uses the `application/json` message content type, but you can change this type as necessary.
116-
117130
* Due to possible concurrently running functions, high volumes, or heavy loads, avoid instantiating the [HTTPClient class](/dotnet/api/system.net.http.httpclient) with the `using` statement and directly creating HTTPClient instances per request. For more information, see [Use HttpClientFactory to implement resilient HTTP requests](/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-class-available-in-net-core).
118131

119132
* If possible, reuse the instance of HTTP clients. For more information, see [Manage connections in Azure Functions](../azure-functions/manage-connections.md).
120133

121-
The following example uses the [`Task.Run` method](/dotnet/api/system.threading.tasks.task.run) in [asynchronous](/dotnet/csharp/language-reference/keywords/async) mode. For more information, see [Asynchronous programming with async and await](/dotnet/csharp/programming-guide/concepts/async/).
134+
The following example uses the [`Task.Run` method](/dotnet/api/system.threading.tasks.task.run) in [asynchronous](/dotnet/csharp/language-reference/keywords/async) mode. For more information, see [Asynchronous programming with async and await](/dotnet/csharp/programming-guide/concepts/async/). The example also uses the `application/json` message content type, but you can change this type as necessary.
122135

123136
```csharp
124137
using System;
125138
using System.Threading.Tasks;
126139
using System.Net.Http;
127140
using System.Text;
128141

129-
// Set up the logic app workflow URI. You can also get it from App Settings or an environment variable.
142+
// 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 an environment variable.
130143
private static string logicAppUri = @"https://prod-05.westus.logic.azure.com:443/workflows/<remaining-callback-URL>";
131144

132145
// Reuse the instance of HTTP clients if possible. For more information, see https://learn.microsoft.com/azure/azure-functions/manage-connections.
@@ -141,21 +154,23 @@ Next, create the function that acts as the trigger and listens to the queue.
141154

142155
## Test your logic app workflow
143156

144-
To test your logic app workflow, use a tool to add a message to your Service Bus queue.
157+
For testing, add a message to your Service Bus queue by using the following steps or other tool:
145158

146-
1. In the Azure portal, open your Service Bus namespace.
159+
1. In the [Azure portal](https://portal.azure.com), open your Service Bus namespace.
147160

148161
1. On the Service Bus namespace navigation menu, select **Queues**.
149162

150-
:::image type="content" source="./media/logic-apps-scenario-function-sb-trigger/service-bus-namespace-queues.png" alt-text="Screenshot of a Service Bus namespace in the Azure portal. On the navigation menu, 'Queues' is highlighted.":::
163+
:::image type="content" source="./media/logic-apps-scenario-function-sb-trigger/service-bus-namespace-queues.png" alt-text="Screenshot of a Service Bus namespace. On the navigation menu, 'Queues' is highlighted.":::
151164

152-
1. Select the queue that you linked to your function through its Service Bus connection.
165+
1. Select the Service Bus queue that you linked to your function earlier using a Service Bus connection.
166+
167+
1. On the queue navigation menu, select **Service Bus Explorer**, and then on the toolbar, select **Send messages**.
153168

154-
1. On the queue navigation menu, select **Service Bus Explorer**, and then select **Send messages**.
155-
156169
:::image type="content" source="./media/logic-apps-scenario-function-sb-trigger/select-service-bus-explorer.png" alt-text="Screenshot of a Service Bus queue page in the portal, with 'Send messages' highlighted. On the navigation menu, 'Service Bus Explorer' is highlighted.":::
157170

158-
1. Use the tool to send a message to your Service Bus queue. The message triggers your logic app workflow.
171+
1. On the **Send messages** pane, specify the message to send to your Service Bus queue.
172+
173+
This message triggers your logic app workflow.
159174

160175
## Next steps
161176

3.86 KB
Loading
-3.26 KB
Loading
12.1 KB
Loading
-3.08 KB
Loading
5.18 KB
Loading
-1.95 KB
Loading
-38.1 KB
Loading

0 commit comments

Comments
 (0)