LogoLogo
GitHub
  • Elsa Workflows 3
  • Getting Started
    • Concepts
      • Outcomes
      • Correlation ID
    • Hello World
    • Prerequisites
    • Packages
    • Containers
      • Docker
      • Docker Compose
        • Elsa Server + Studio
        • Elsa Server + Studio - Single Image
        • Persistent Database
        • Traefik
  • Application Types
    • Elsa Server
    • Elsa Studio
    • Elsa Server + Studio (WASM)
  • Guides
    • HTTP Workflows
      • Programmatic
      • Designer
    • External Application Interaction
    • Loading Workflows from JSON
    • Running Workflows
      • Using Elsa Studio
      • Using a Trigger
      • Dispatch Workflow Activity
  • Activities
    • Control Flow
      • Decision
    • MassTransit
      • Tutorial
  • Expressions
    • C#
    • JavaScript
    • Python
    • Liquid
  • Extensibility
    • Custom Activities
  • Reusable Triggers (3.5-preview)
  • Multitenancy
    • Introduction
    • Setup
  • Operate
    • Variables
    • Activation Strategies
    • Incidents
      • Strategies
      • Configuration
    • Alterations
      • Alteration Plans
        • REST API
      • Applying Alterations
        • REST API
        • Extensibility
  • Optimize
    • Log Persistence
    • Retention
  • Hosting
    • Distributed Hosting
Powered by GitBook
On this page
  • Installing the C# Feature
  • Configuration
  • Globals
  • WorkflowInstanceId
  • CorrelationId
  • Variable
  • Output
  • Input
  • Adding Assemblies and Namespaces
Edit on GitHub
  1. Expressions

C#

In this section, we cover some of the built-in variables and functions available to the C# expression syntax.

When creating workflows, you'll often need to write dynamic expressions. This page provides an overview of enabling C# expressions and what objects you can use.

Installing the C# Feature

The C# Expressions feature is provided by the following package:

dotnet package add Elsa.CSharp

You can enable the feature as follows:

Program.cs
services.AddElsa(elsa =>
{
   elsa.UseCSharp();
});

Configuration

The UseCSharp extension provides an overload that accepts a delegate that lets you configure CSharpOptions. These options let you configure what assemblies and namespaces to make available to C# expressions, and provides a way to register additional reusable global methods.

For example:

Program.cs
services.AddElsa(elsa =>
{
   elsa.UseCSharp(options =>
   {
      // Make available additional assemblies.
      options.Assemblies.Add(GetType().Assembly);
      
      // Make available additional assemblies.
      options.Namespaces.Add(typeof(MyEntity).Namespace!);
   
      // Register a global method called 'Greet'.
      options.AppendScript("string Greet(string name) => $\"Hello {name}!\";");
   });
});

Out of the box, the following namespaces are available:

  • System

  • System.Collections.Generic

  • System.Linq

  • System.Text.Json

  • System.Text.Json.Serialization

  • System.Text.Nodes

Globals

The following members are available as globals to all C# expressions:

WorkflowInstanceId

Type: String.

The WorkflowInstanceId property returns the workflow instance ID of the currently executing workflow.

Example usage:

return WorkflowInstanceId;

CorrelationId

Type: String.

The CorrelationId property gets or sets the correlation ID of the currently executing workflow.

Example usage:

CorrelationId = Guid.New().ToString();

Variable

The Variable object provides access to the following methods and properties related to accessing workflow variables:

/// <summary>
/// Gets the value of the workflow variable as specified by name.
/// </summary>
T? Get<T>(string name);

/// <summary>
/// Gets the value of the workflow variable as specified by name.
/// </summary>
object? Get(string name);

/// <summary>
/// Sets the value of the workflow variable as specified by name.
/// </summary>
void Set(string name, object? value);

In addition, the Variable object provides strongly-typed access to all workflow variables. For example, if your workflow defines a variable called OrderId of type Guid, the following property will be available on the Variable object:

Guid OrderId { get; set; }

Output

The Output object provides access to the following methods and properties related to accessing activity output:

/// <summary>
/// Gets the value of the specified output from the specified activity.
/// </summary>
/// <param name="activityIdOrName">The ID or name of the activity that produced the output.</param>
/// <param name="outputName">The name of the output.</param>
/// <returns>The value of the output.</returns>
object? From(string activityIdOrName, string? outputName = null);

/// <summary>
/// Gets the value of the specified output from the specified activity.
/// </summary>
/// <param name="activityIdOrName">The ID or name of the activity that produced the output.</param>
/// <param name="outputName">The name of the output.</param>
/// <returns>The value of the output.</returns>
T? From<T>(string activityIdOrName, string? outputName = null);

/// <summary>
/// Gets the result of the last activity that executed.
/// </summary>
object? LastResult { get; }

Input

The Input object provides access to the following methods related to accessing workflow input:

/// <summary>
/// Gets the value of the specified input.
/// </summary>
object? Get(string name);

/// <summary>
/// Gets the value of the specified input.
/// </summary>
T? Get<T>(string name);

In addition, the Input object provides strongly-typed access to all workflow-defined inputs. For example, if your workflow defines an input called OrderNumber of type string, the following property will be available on the Input object:

string Ordernumber { get; }

Adding Assemblies and Namespaces

You can make available additional assemblies and namespaces to C# expressions by configuring the C# Expression Feature from your application's startup code. For example:

PreviousTutorialNextJavaScript

Last updated 5 months ago

Elsa uses to implement the C# expression evaluator.

Roslyn
WorkflowInstanceId
CorrelationId
Variable
Output
Input