Embedded CSharp Scripts v2

Use a C# Script when Workflow Steps and mappings cannot express the logic you need. Scripts can reference NuGet packages. Common uses include:

  • Converting data with an unknown schema into a defined shape that later Steps can map.
  • Implementing an algorithm not provided by a Node, such as a specialized encryption or signing method.
  • Calling an endpoint without a dedicated connector when authentication or request handling would otherwise require several Steps.

Add Script

Add the C# Script Node and start with one of its templates:

  • Basic scalar method for ordinary inputs and one result.
  • Object method for a typed object and several named Returns.
  • Large collection example for lazy IAsyncEnumerable<T> processing.

The Script.Content value is a complete C# source file. It must contain a namespace, exactly one public type decorated with the FgNode attribute, and exactly one Method decorated with the FgMethod.Invoke attribute.

The compiler reads the Method signature and replaces the C# Step's dynamic Parameters and Returns with that contract. When you change a matching type, Flowgear retains compatible authored Values, Expressions, and Samples; fields that no longer exist are removed from the refreshed shape.

Minimal source shape

```csharp`
using Flowgear.Sdk;

namespace Flowgear.Scripts;

[FgNode("v2.NormalizeName", "Normalize Name", SupportedClusters.Anywhere)]
public sealed class NormalizeName
{
[FgMethod.Invoke(
NodeTypes.Processor,
"Execute",
"Normalizes a supplied name.",
"Result")]
public Task ExecuteAsync(
[FgParameter("Name", "The name to normalize.")] string name,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.FromResult(name.Trim());
}
}


Flowgear supplies the `CancellationToken`; it does not appear as a Workflow Parameter. Use it in asynchronous or long-running work so `Stop` and execution deadlines can cancel the Method cooperatively.

The compiler automatically imports common System namespaces for collections, IO, LINQ, HTTP, threading, and tasks. Add `using Flowgear.Sdk;` and any other required namespaces to the source.

# Supported contracts

Use ordinary attributed Parameters and supported Task or asynchronous-enumerable Returns. Script-defined classes can expose Object and Array shapes, and `FgReturnContainer` with `FgReturn` can expose several top-level Returns.

# Preview, Debug, and publish

Preview compiles only the declaration shape, does not download NuGet packages, and never constructs or invokes the authored class. It can therefore validate the exposed contract without proving that package-dependent Method code compiles.

Debug and save/publish compilation use the complete source and exact package dependencies. Flowgear embeds the script into a Step-specific namespace in the compiled Workflow. Package runtime files are included in the published Workflow artifact when required.

At runtime, Flowgear constructs the embedded class for each Step invocation. Its values use ordinary mapping, logging, deferred-data, error, and cancellation behavior.

# Security and governance

An embedded script is executable code, not a sandboxed mapping Expression. Its source and NuGet dependencies execute inside the Runtime process. Someone who can change and publish the Workflow can change that code.

Review source, packages, network/file access, logging, and returned data before promotion. Pin every dependency to an exact version and repeat the review when the version changes. Prefer a purpose-built published Node when the capability needs reuse, Connection injection, dedicated lifecycle Methods, or central governance across many Workflows.

# Convert a Script to a Node

Script Node content is intended to make it easy to build a custom Node. See [Custom Nodes](/v2/features/custom-nodes) for more information.

# See also

See [Use NuGet Packages in a Script](/v2/guides/use-nuget-packages-in-a-script), [Compiled execution](/v2/concepts/compiled-execution), [Activation Modes and Timeouts](/v2/concepts/activation-modes-and-timeouts), and [Workflow Log Redaction](/v2/features/workflow-log-redaction).