Skip to content

Commit 5aa4102

Browse files
author
ecfan
committed
Draft
1 parent 089a35a commit 5aa4102

File tree

3 files changed

+125
-12
lines changed

3 files changed

+125
-12
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Add and run C# scripts in Standard workflows
3+
description: Write and run C# scripts inline from Standard workflows to perform custom integration tasks using Inline Code operations in Azure Logic Apps.
4+
ms.service: logic-apps
5+
ms.suite: integration
6+
ms.reviewer: estfan, kewear, azla
7+
ms.topic: how-to
8+
ms.date: 06/10/2024
9+
# Customer intent: As a logic app workflow developer, I want to write and run my own C# scripts so that I can perform custom integration tasks in Standard workflows for Azure Logic Apps.
10+
---
11+
12+
# Add and run C# scripts inline with Standard workflows for Azure Logic Apps (Preview)
13+
14+
[!INCLUDE [logic-apps-sku-standard](~/reusable-content/ce-skilling/azure/includes/logic-apps-sku-standard.md)]
15+
16+
> [!NOTE]
17+
> This capability is in preview and is subject to the
18+
> [Supplemental Terms of Use for Microsoft Azure Previews](https://azure.microsoft.com/support/legal/preview-supplemental-terms/).
19+
20+
To perform custom integration tasks inline with your Standard workflow in Azure Logic Apps, you can directly add and run simple C# scripts from your workflow in the Azure portal. For this task, use the **Inline Code** action named **Execute CSharp Script Code**. This action returns the result from the script so you can use that output in your workflow's subsequent actions.
21+
22+
This capability provides the following benefits:
23+
24+
- Write your own scripts to solve more complex integration problems without having to separately provision Azure Functions.
25+
26+
This benefit streamlines workflow development plus reduces the complexity and cost with managing more services.
27+
28+
- Deploy scripts alongside your workflows. No other service plans are necessary.
29+
30+
This guide shows how to add the action in your workflow and add the C# script code that you want to run.
31+
32+
## Prerequisites
33+
34+
* An Azure account and subscription. If you don't have a subscription, [sign up for a free Azure account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
35+
36+
* The Standard logic app workflow where you want to add your C# script. The workflow must already start with a trigger. For more information, see [Create example Standard logic app workflows](create-single-tenant-workflows-azure-portal.md).
37+
38+
You can use any trigger for your scenario, but as an example, this guide uses the **Request** trigger named **When a HTTP request is received** and also the **Response** action. The workflow runs when another application or workflow sends a request to the trigger's endpoint URL. The sample script returns the results from code execution as output that you can use in subsequent actions.
39+
40+
## Considerations
41+
42+
- The Azure portal saves your script as a .csx file in the same folder as your **workflow.json** file, which stores the JSON definition for your workflow, and deploys the file to your logic app resource along with the workflow definition. Azure Logic Apps compiles this file to make the script ready for execution.
43+
44+
You can rename the .csx file for easier management during deployment. However, each time you rename the script, the new version overwrites the previous version.
45+
46+
- The script is local to the workflow. To use the same script in other workflows, in the Kudo console, find the script file, open the shortcut menu, and then copy the script to reuse in other workflows.
47+
48+
## Add the Execute CSharp Script Code action
49+
50+
1. In the [Azure portal](https://portal.azure.com), open your Standard workflow in the designer.
51+
52+
1. In the designer, [follow these general steps to add the **Inline Code Operations** action named **Execute CSharp Script Code action** to your workflow](create-workflow-with-trigger-or-action.md?tabs=standard#add-action).
53+
54+
1. After the action information pane opens, on the **Parameters** tab, in the **Code File** box, enter your script, for example:
55+
56+
:::image type="content" source="media/add-run-csharp-scripts/action-sample-script.png" alt-text="Screenshot shows Azure portal, Standard workflow designer, Request trigger, Execute CSharp Script Code action with information pane open, and Response action. Information pane shows sample C# script." lightbox="media/add-run-csharp-scripts/action-sample-script.png":::
57+
58+
This example continues with the following sample script code:
59+
60+
```csharp
61+
// Add the required libraries.
62+
#r "Newtonsoft.Json"
63+
#r "Microsoft.Azure.Workflows.Scripting"
64+
using Microsoft.AspNetCore.Mvc;
65+
using Microsoft.Extensions.Primitives;
66+
using Microsoft.Extensions.Logging;
67+
using Microsoft.Azure.Workflows.Scripting;
68+
using Newtonsoft.Json.Linq;
69+
70+
/// <summary>
71+
/// Executes the inline C# code.
72+
/// </summary>
73+
/// <param name="context">The workflow context.</param>
74+
/// <remarks> The entry-point to your code. The function signature should remain unchanged.</remarks>
75+
public static async Task<Results> Run(WorkflowContext context, ILogger log)
76+
{
77+
var triggerOutputs = (await context.GetTriggerResults().ConfigureAwait(false)).Outputs;
78+
79+
//// Dereference the 'name' property from the trigger payload.
80+
var name = triggerOutputs?["body"]?["name"]?.ToString();
81+
82+
//// To get the action outputs from a preceding action, you can uncomment and use the following code.
83+
//var actionOutputs = (await context.GetActionResults("Compose").ConfigureAwait(false)).Outputs;
84+
85+
//// The following logs appear in the Application Insights traces table.
86+
//log.LogInformation("Outputting results.");
87+
88+
//var name = null;
89+
90+
return new Results
91+
{
92+
Message = !string.IsNullOrEmpty(name) ? $"Hello {name} from CSharp action" : "Hello from CSharp action."
93+
};
94+
}
95+
96+
public class Results
97+
{
98+
public string Message {get; set;}
99+
}
100+
```
101+
102+
103+
1. When you're done, save your workflow.
104+
105+
## View the script file
106+
107+
108+
109+
## How scripting works in this scenario
110+
111+
The .csx format allows you to write less "boilerplate" and focus on writing just a C# function. Instead of wrapping everything in a namespace and class, just define a Run method. Include any assembly references and namespaces at the beginning of the file as usual. The name of this method is predefined, and your workflow can run only invoke this Run method at runtime.
112+
113+
Data from your workflow flows into your Run method through parameter of WorkflowContext type. In addition to the workflow context, you can also have this method take function logger as a parameter and a cancellation tokens (needed if your script is long running and needs to be gracefully terminate in case of Function Host is shutting down).
114+
115+
## Related content

articles/logic-apps/logic-apps-add-run-inline-code.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
---
2-
title: Run code snippets in workflows
3-
description: Run code snippets in workflows using Inline Code operations in Azure Logic Apps.
2+
title: Add and run JavaScript code snippets in workflows
3+
description: Write and run JavaScript code snippets in workflows to perform custom integration tasks using Inline Code operations in Azure Logic Apps.
44
services: logic-apps
55
ms.suite: integration
66
ms.reviewer: estfan, azla
77
ms.topic: how-to
8-
ms.date: 10/10/2023
8+
ms.date: 06/10/2024
99
ms.custom: devx-track-js
10+
# Customer intent: As a logic app workflow developer, I want to write and run my own JavaScript code snippets so that I can perform custom integration tasks in Standard workflows for Azure Logic Apps.
1011
---
1112

12-
# Run code snippets in workflows with Inline Code operations in Azure Logic Apps
13+
# Add and run JavaScript code snippets inline with workflows for Azure Logic Apps
1314

1415
[!INCLUDE [logic-apps-sku-consumption-standard](~/reusable-content/ce-skilling/azure/includes/logic-apps-sku-consumption-standard.md)]
1516

16-
To create and run a code snippet in your logic app workflow without much setup, you can use the **Inline Code** built-in connector. This connector has an action that returns the result from the code snippet so that you can use that output in your workflow's subsequent actions.
17-
18-
Currently, the connector only has a single action, which works best for a code snippet with the following attributes, but more actions are in development. The **Inline Code** built-in connector also has
19-
[different limits](logic-apps-limits-and-config.md#inline-code-action-limits), based on whether your logic app workflow is [Consumption or Standard](logic-apps-overview.md#resource-environment-differences).
17+
To perform custom integration tasks inline with your workflow in Azure Logic Apps, you can directly add and run simple JavaScript code snippets from your workflow in the Azure portal. For this task, use the **Inline Code** action named **Execute JavaScript Code**. This action returns the result from the code snippet so you can use that output in your workflow's subsequent actions. This action also has [different limits](logic-apps-limits-and-config.md#inline-code-action-limits), based on whether you have a [Consumption or Standard workflow](logic-apps-overview.md#resource-environment-differences) and works best for code snippets with the following attributes:
2018

2119
| Action | Language | Language version | Run duration | Data size | Other notes |
2220
|--------|----------|------------------|--------------|-----------|-------------|
2321
| **Execute JavaScript Code** | JavaScript | **Standard**: <br>Node.js 16.x.x <br><br>**Consumption**: <br>Node.js 8.11.1 <br><br>For more information, review [Standard built-in objects](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects). | Finishes in 5 seconds or fewer. | Handles data up to 50 MB. | - Doesn't require working with the [**Variables** actions](logic-apps-create-variables-store-values.md), which are unsupported by the action. <br><br>- Doesn't support the `require()` function for running JavaScript. |
2422

25-
To run code that doesn't fit these attributes, you can [create and call a function through Azure Functions](logic-apps-azure-functions.md) instead.
23+
To run code that doesn't fit these attributes, you can [create and call a function using Azure Functions](logic-apps-azure-functions.md).
2624

27-
This article shows how the action works in an example workflow that starts with an Office 365 Outlook trigger. The workflow runs when a new email arrives in the associated Outlook email account. The sample code snippet extracts any email addresses that exist the email body and returns those addresses as output that you can use in a subsequent action.
25+
This guide shows how the action works in an example workflow that starts with an Office 365 Outlook trigger. The workflow runs when a new email arrives in the associated Outlook email account. The sample code snippet extracts any email addresses that exist the email body and returns those addresses as output that you can use in a subsequent action.
2826

2927
The following diagram shows the highlights from example workflow:
3028

3129
### [Consumption](#tab/consumption)
3230

33-
![Screenshot showing an example Consumption logic app workflow with the Execute JavaScript Code action.](./media/logic-apps-add-run-inline-code/inline-code-overview-consumption.png)
31+
:::image type="content" source="media/logic-apps-add-run-inline-code/inline-code-overview-consumption.png" alt-text="Screenshot shows example Consumption workflow with the Execute JavaScript Code action." lightbox="media/logic-apps-add-run-inline-code/inline-code-overview-consumption.png":::
3432

3533
### [Standard](#tab/standard)
3634

37-
![Screenshot showing an example Standard logic app workflow with the Execute JavaScript Code action.](./media/logic-apps-add-run-inline-code/inline-code-overview-standard.png)
35+
:::image type="content" source="media/logic-apps-add-run-inline-code/inline-code-overview-standard.png" alt-text="Screenshot shows example Standard workflow with the Execute JavaScript Code action." lightbox="media/logic-apps-add-run-inline-code/inline-code-overview-standard.png":::
3836

3937
---
4038

105 KB
Loading

0 commit comments

Comments
 (0)