You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/logic-apps/logic-apps-scenario-function-sb-trigger.md
+75-60Lines changed: 75 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,42 +1,47 @@
1
1
---
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.
4
4
services: logic-apps
5
5
ms.suite: integration
6
6
ms.reviewer: estfan, azla
7
7
ms.topic: how-to
8
8
ms.date: 10/31/2022
9
9
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.
11
11
---
12
12
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
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.
18
18
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.
20
27
21
28
## Prerequisites
22
29
23
30
* 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).
24
31
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)
26
33
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.
28
35
29
36
* Basic knowledge about [how to create a Consumption logic app workflow](quickstart-create-first-logic-app-workflow.md).
30
37
31
38
## Create a logic app workflow
32
39
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.
34
41
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**.
36
43
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**.
40
45
41
46
:::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.":::
42
47
@@ -54,79 +59,87 @@ For this scenario, you have a function running for each logic app workflow that
54
59
55
60
:::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.":::
56
61
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
+
}
80
86
}
81
87
}
82
88
}
83
89
}
84
-
}
85
-
```
90
+
```
86
91
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.
88
93
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.
90
95
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.
92
97
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.
94
99
95
100
:::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.":::
96
101
97
102
## Create a function
98
103
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.
100
105
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.
102
107
103
108
1. On the function app navigation menu, select **Functions**. On the **Functions** pane, select **Create**.
104
109
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.":::
106
111
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:
108
113
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. |
110
119
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.":::
112
121
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.
114
129
115
-
* The following example uses the `application/json` message content type, but you can change this type as necessary.
116
-
117
130
* 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).
118
131
119
132
* If possible, reuse the instance of HTTP clients. For more information, see [Manage connections in Azure Functions](../azure-functions/manage-connections.md).
120
133
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.
122
135
123
136
```csharp
124
137
using System;
125
138
using System.Threading.Tasks;
126
139
using System.Net.Http;
127
140
using System.Text;
128
141
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.
// 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.
141
154
142
155
## Test your logic app workflow
143
156
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:
145
158
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.
147
160
148
161
1. On the Service Bus namespace navigation menu, select **Queues**.
149
162
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.":::
151
164
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**.
153
168
154
-
1. On the queue navigation menu, select **Service Bus Explorer**, and then select **Send messages**.
155
-
156
169
:::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.":::
157
170
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.
0 commit comments