|
| 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 |
0 commit comments