Introducing a new way to review your PRs.

Read the announcement

elsa-workflows / elsa-core

The Workflow Engine for .NET

View on GitHub

32 documents · Updated Oct 7, 2026

docs / workflow-engine

Workflow engine architecture

Elsa is a workflow engine that turns a workflow definition into an executable activity graph. The core runs that graph and tracks its state. Runtime services start, dispatch, pause, resume, cancel, and recover workflow instances. Management services make definitions available and turn stored definitions into executable graphs. API endpoints expose these capabilities to external clients without moving execution rules into the API layer.

System boundaries

The engine separates the work of running a graph from the work of finding a graph and delivering requests to it. This keeps the execution model usable from code, background services, and HTTP clients.

Core execution

The core execution layer owns the meaning of an activity and the rules for advancing a workflow. It receives a WorkflowGraph, creates a WorkflowExecutionContext, and uses the activity and workflow pipelines to produce state and outputs. It does not decide how a stored definition is selected or how a remote request reaches the process. See Workflow and activity execution for the detailed execution model.

References: src/…/WorkflowRunner.cs• L16-27 src/…/WorkflowRunner.cs• L70-97 src/…/IWorkflowExecutionPipeline.cs• L5-30 src/…/IActivityExecutionPipeline.cs• L5-26

Runtime services

The runtime layer turns workflow operations into instance-level actions. A client identifies an instance. A dispatcher can deliver work through a queue. Bookmark and trigger services find work that can start or resume. Recovery services use saved execution state to continue work after an interruption. See Workflow runtime dispatch and recovery for dispatch, persistence, and recovery behavior.

References: src/…/IWorkflowRuntime.cs• L13-34 src/…/IWorkflowClient.cs• L6-51 src/…/IWorkflowDispatcher.cs• L6-30 src/…/WorkflowResumer.cs• L87-127

Definition management

The management layer owns the durable meaning of a workflow definition. It supports versioned definitions, validation, publication, and lookup. Its key boundary is materialization: a stored definition becomes an executable WorkflowGraph before the core can run it. See Workflow definition management for authoring, versioning, and materialization.

References: src/…/WorkflowDefinition.cs• L7-103 src/…/IWorkflowDefinitionService.cs• L10-18 src/…/IWorkflowDefinitionPublisher.cs• L9-45 src/…/WorkflowManagementFeature.cs• L270-304

API exposure

The API layer exposes management and runtime contracts as protected HTTP operations. It selects a definition or instance, checks permissions, and calls the relevant management or runtime service. The API is not an alternate execution engine: direct execution and queued dispatch both end at the same runtime and core execution boundaries.

References: src/…/WorkflowsApiFeature.cs• L18-47 src/…/PostEndpoint.cs• L10-20 src/…/Endpoint.cs• L12-22 src/…/Endpoint.cs• L8-24

Workflow model

A workflow is a named, versioned arrangement of activities. The engine turns that arrangement into a graph so that execution can address each activity by stable identity and can keep execution state separate from the definition.

Activity model

An activity is the smallest unit of work in a workflow. It has a logical type and version, accepts evaluated input, produces output, and can schedule more activities. Activities can also wait by creating a bookmark, which gives the runtime a durable point at which to resume the instance later.

The Activity base class is the main extension point for code activities. Derived activities provide execution behavior and can use behaviors or signal handlers for shared or follow-up work. Activity descriptors describe the type for construction, input and output mapping, graph tools, and activity selection.

Container activities coordinate child work. Sequence is a container that schedules its children in order and completes when all children finish. A child can stop the sequence through a break signal, and a terminal child can complete the sequence early. A rescheduled child keeps the sequence as the owner of its completion callback, so the container can continue or finish the same sequence after that child completes. src/…/Sequence.cs• L10-15 src/…/Sequence.cs• L32-73

State-machine activities model named states and transitions. The state machine enters its initial or restored state, runs entry work, evaluates automatic transitions, and waits for trigger activities when needed. A valid transition can run exit and transition actions before changing the current state. Competing triggers are canceled after a transition wins, while automatic cycles yield back to the scheduler between iterations. src/…/StateMachine.cs• L15-20 src/…/StateMachine.cs• L60-123 src/…/StateMachine.cs• L140-224

In token-based flow, completed activities emit tokens for their active outcomes. A target activity uses its merge mode to decide whether to run on one path, wait for several paths, or race competing paths. This lets a workflow express normal sequence, branch synchronization, streaming, and first-result behavior within the same activity model.

References: src/…/IActivity.cs• L5-60 src/…/Activity.cs• L12-37 src/…/Activity.cs• L96-133 src/…/Activity.cs• L173-207 src/…/ActivityDescriptor.cs• L7-119 src/…/Sequence.cs• L20-97 src/…/StateMachine.cs• L76-224 src/…/Flowchart.Tokens.cs• L13-57 src/…/MergeMode.cs• L7-52

Activity serialization

Activity serialization preserves an activity as a JSON object that can be stored or exchanged and later rebuilt through the activity registry. Normal activity properties use the shared JSON model. Descriptor-defined synthetic inputs and outputs are added to that model so the serialized form includes activity data that does not exist as ordinary CLR properties.

Activity descriptors can adjust the JSON options for their type. The serializer applies those options only to the activity being written, then checks that the result is an object with no duplicate property names. This keeps activity-specific formats compatible with the common serializer while making invalid activity representations fail early.

References: src/…/JsonActivitySerializer.cs• L11-49 src/…/JsonActivitySerializer.cs• L52-67 src/…/SyntheticPropertiesWriter.cs• L8-20 src/…/ActivityDescriptor.cs• L121-137

Workflow definitions

An IWorkflow is a definition source that builds a workflow through an IWorkflowBuilder. The builder gives the definition a stable identity and version. It also declares its root activity, inputs, outputs, variables, and options. WorkflowBase provides the usual code-first form, while stored definitions use the management layer to reach the same executable model.

References: src/…/IWorkflow.cs• L3-12 src/…/IWorkflowBuilder.cs• L7-85 src/…/WorkflowBase.cs• L6-45 src/…/WorkflowDefinition.cs• L10-70

Workflow graphs

The graph is the execution-ready view of a workflow. It contains a root node and a flat set of activity nodes with lookups by node identity and activity. Graph construction assigns identities after visiting the activity structure. This lets execution state refer to activities without changing the workflow definition itself.

References: src/…/WorkflowGraph.cs• L7-54 src/…/WorkflowGraphBuilder.cs• L7-18

Execution contexts

A WorkflowExecutionContext represents one running or suspended workflow instance. It carries the graph, instance identity, inputs, outputs, variables, bookmarks, incidents, status, active activity contexts, and execution log. An ActivityExecutionContext represents one activity execution within that instance. It carries activity state, evaluated input, output, parent and child relationships, and the link to the activity that scheduled it.

The two contexts separate structure from time. A parent context describes where an activity sits in the graph. A scheduling link describes which execution caused it to run. This distinction supports containers, branching, nested workflows, and useful execution history without changing the graph shape.

References: src/…/WorkflowExecutionContext.cs• L28-87 src/…/WorkflowExecutionContext.cs• L242-405 src/…/ActivityExecutionContext.cs• L15-59 src/…/ActivityExecutionContext.cs• L100-148 src/…/ActivityExecutionContext.cs• L241-302

Execution lifecycle

Execution moves from a definition to a graph, then to a context, and finally to committed workflow state. The same core runner handles a new instance and a resumed instance. The difference is which work is placed on the scheduler before the run starts.

Execution path

The main execution path follows these stages:

  1. A code workflow or management service provides a Workflow or WorkflowGraph.
  2. The runner creates or restores a workflow execution context.
  3. The context schedules the root activity or the work selected for a resume.
  4. The workflow pipeline advances scheduled work. The activity pipeline invokes each activity.
  5. The runner extracts workflow state and commits it after execution. It also returns outputs and a journal for the caller.

This path makes the runner the boundary between the pure execution model and state handling around an execution cycle. The runtime can call it directly or reach it through a client, while the API only selects and authorizes the requested operation.

The following diagram shows the main path from a definition source to a result or saved state.

flowchart LR
    A[Code or stored definition] --> B[Workflow or WorkflowGraph]
    B --> C[WorkflowExecutionContext]
    C --> D[Workflow scheduler]
    D --> E[Activity execution pipeline]
    E --> F{Activity completes or waits}
    F -->|More work| D
    F -->|Waits| G[Bookmarks and saved state]
    F -->|Completes| H[Outputs, journal, and committed state]

References: src/…/WorkflowRunner.cs• L30-42 src/…/WorkflowRunner.cs• L70-97 src/…/WorkflowRunner.cs• L115-203 src/…/WorkflowRunner.cs• L207-257 src/…/IActivityScheduler.cs• L5-25

Suspension and resumption

An activity suspends progress by creating a bookmark. A bookmark records what can resume the activity and connects that wait point to the workflow instance. The runtime matches a later stimulus to stored bookmarks, creates a client for each matching instance, and asks the client to run the instance with the selected bookmark.

This boundary lets the core describe a wait without knowing whether the future stimulus comes from an HTTP request, a message, a timer, or another process. The runtime owns lookup, delivery, and coordination. The core owns what happens after the activity resumes.

References: src/…/ActivityExecutionContext.cs• L397-433 src/…/ActivityExecutionContext.cs• L437-533 src/…/WorkflowResumer.cs• L29-39 src/…/WorkflowResumer.cs• L87-127

Persisted execution state

Persisted execution state is the durable form of a workflow context between execution cycles. The state extractor records identity, status, input, output, bookmarks, incidents, active activity contexts, completion callbacks, and scheduled work. On restore, it rebuilds activity context links and puts scheduled work back on the scheduler.

The scheduler state preserves more than the next activity. Each serialized work item identifies the target activity, its owner context, optional existing context, input, variables, tag, and the scheduling relationship. These links allow a resumed workflow to preserve callback ownership and activity nesting rather than start the work as an unrelated invocation. src/…/ActivityWorkItemState.cs• L5-53

Restore can skip state references that no longer exist in the selected graph. The extractor records these cases as warnings and marks them as migration-compatible when the persisted definition version differs from the target version. This permits controlled definition migration while making unexpected state loss visible.

References: src/…/WorkflowStateExtractor.cs• L10-67 src/…/WorkflowStateExtractor.cs• L105-175 src/…/WorkflowStateExtractor.cs• L178-270 src/…/WorkflowStateExtractor.cs• L290-347 src/…/ActivityWorkItemState.cs• L5-53

Extension points

The engine is extended through contracts at the same boundaries that define its architecture. New activity types extend the work model. Pipeline contributors add cross-cutting execution behavior. Providers and materializers connect the engine to new sources of activities and definitions. Runtime contracts allow different delivery and storage choices without changing activity code.

Activity types

An activity type can be added by implementing IActivity or deriving from Activity. Its descriptor makes the type known to the engine and describes how it can be created and connected. IActivityProvider is the extension point for supplying descriptors, while IActivityRegistry makes the available types discoverable and resolves the correct type and version for execution.

References: src/…/IActivityProvider.cs• L5-13 src/…/IActivityRegistry.cs• L6-16 src/…/IActivityRegistry.cs• L67-93 src/…/ActivityRegistry.cs• L10-24 src/…/ActivityRegistry.cs• L158-176

Execution pipelines

Execution pipelines are ordered middleware chains around a workflow or an activity. A pipeline contributor can add behavior such as fault handling, logging, notifications, or state capture without placing that concern in every activity. Workflow-level contributors affect the execution cycle. Activity-level contributors affect one activity invocation.

The default core feature builds both pipelines and provides the runner and invoker that use them. Applications can replace or extend the pipeline configuration when they need a cross-cutting rule that applies to a whole execution boundary.

References: src/…/IWorkflowExecutionPipelineContributor.cs• L5-18 src/…/IActivityExecutionPipelineContributor.cs• L5-18 src/…/WorkflowsFeature.cs• L85-100 src/…/WorkflowsFeature.cs• L242-250 src/…/ActivityInvoker.cs• L8-21 src/…/ActivityInvoker.cs• L43-65

Runtime and definition providers

Runtime services are replaceable at the delivery boundary. IWorkflowRuntime creates instance clients. IWorkflowDispatcher represents queued delivery. Stores and recovery services provide the durable coordination needed by long-running work. The default local runtime is for one node and does not provide clustering.

Definition materializers are replaceable at the source boundary. IWorkflowMaterializer converts stored definition data into a Workflow. The definition service then builds or returns a graph for the core runner. This keeps storage formats and authoring tools separate from execution semantics.

References: src/…/LocalWorkflowRuntime.cs• L5-10 src/…/IWorkflowRuntime.cs• L13-34 src/…/IWorkflowDispatcher.cs• L6-30 src/…/IWorkflowMaterializer.cs• L7-22 src/…/IWorkflowDefinitionService.cs• L16-18

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to activity identity, type version, or graph construction must remain compatible with saved activity execution state and resume handles.
  • Changes to bookmark names, payload hashing, or activity instance identity must be checked against runtime bookmark lookup and distributed resume coordination.
  • Changes to flowchart outcomes, token consumption, or merge modes must preserve the intended branch, join, loop, and race behavior for existing definitions.
  • Changes to Sequence scheduling or break handling must preserve child completion ownership and ordered progress for resumed sequences.
  • Changes to state-machine transition keys, trigger identities, or automatic-cycle handling must preserve transition completion matching and scheduler ordering.
  • Changes to WorkflowRunner or either execution pipeline must preserve the state extraction and commit boundary used by both direct runs and runtime-driven runs.
  • Changes to activity serialization options, synthetic property shape, or property-name validation must remain compatible with stored and exchanged activity definitions.
  • Changes to definition materialization must preserve the mapping from a versioned WorkflowDefinition to the activity types and graph identities expected by execution.
  • Changes to runtime client or dispatcher contracts must be reflected in API operations that start, dispatch, resume, or cancel instances.
  • Changes to activity descriptors or registry scope must preserve tenant-specific lookup and the type and version used to construct an activity.

Important failure modes

TriggerConsequenceGuard
A workflow graph cannot resolve an activity descriptorThe activity cannot be created or executedRegister the activity type and refresh the relevant descriptor provider before running the graph
A stored definition materializes with an unsupported activity type or invalid graphThe instance cannot start or resumeValidate before publication and use a materializer that supports the definition format
An activity serializer produces a non-object result or duplicate property namesThe activity cannot be safely stored or exchangedKeep activity output object-shaped and validate descriptor-driven properties against the shared JSON options
A synthetic input needs an expression type that has no registered descriptorActivity serialization fails before the definition can be savedRegister the expression type before using it in a synthetic input
A flowchart uses a strict merge or converge point without every required inbound pathThe target activity remains blocked because an untaken path cannot provide its tokenChoose a merge mode that matches the branch design and ensure required paths can execute
A sequence loses its owner or completion callback during restoreThe sequence can stop after a child or fail to advance to the next childPersist the owner context and scheduling links for each work item and callback
A state-machine transition reuses a trigger instance or trigger IDTrigger completion can match the wrong transition or fail validationGive each transition its own trigger activity with a unique identity
A state-machine transition completes after its source state is no longer currentStale trigger or action work can affect a later stateCheck the current source before taking a transition and cancel competing triggers
A bookmark stimulus does not match the stored bookmark identity or hashA waiting workflow remains suspendedKeep bookmark name, payload serialization, and instance matching stable across producers and consumers
The process stops while a workflow or activity is marked as executingThe instance may remain interrupted until recovery runsPersist execution state and let the runtime recovery service reschedule interrupted work
A restored activity context or callback points to a removed graph nodePart of the saved execution path is skipped and the workflow may not resume as intendedTreat definition changes as migrations and review unresolved-state warnings
The local runtime is used across multiple nodesDispatch and instance ownership are not coordinated as a clusterUse a distributed runtime and shared coordination services for multi-node deployment
An API request bypasses permission or read-only checksA caller can execute or change a workflow outside its allowed boundaryKeep endpoint authorization and management/runtime service checks aligned

Key files

FileWhy to read it
src/…/IActivity.csDefines the activity unit that the engine executes.
src/…/IWorkflow.csDefines the code-first workflow definition boundary.
src/…/WorkflowGraph.csShows the execution-ready graph view and its identity lookups.
src/…/WorkflowExecutionContext.csShows the state carried by one workflow execution.
src/…/ActivityExecutionContext.csShows the state and relationships carried by one activity execution.
src/…/Sequence.csShows ordered child execution and completion ownership in a container.
src/…/StateMachine.csShows state entry, trigger-driven transitions, and transition coordination.
src/…/Flowchart.Tokens.csShows how outcome tokens advance connected activities and apply merge behavior.
src/…/WorkflowRunner.csShows how the core starts, resumes, executes, and commits a workflow.
src/…/WorkflowStateExtractor.csShows how execution state and scheduler work are saved and restored.
src/…/ActivityWorkItemState.csDefines the persisted links for scheduled activity work.
src/…/JsonActivitySerializer.csShows the boundary for activity JSON storage and exchange.
src/…/SyntheticPropertiesWriter.csShows how descriptor-defined activity properties join the normal JSON representation.
src/…/WorkflowsFeature.csShows the core execution services and pipeline extension points.
src/…/IWorkflowClient.csDefines instance-level runtime operations.
src/…/IWorkflowDispatcher.csDefines queued workflow delivery operations.
src/…/WorkflowResumer.csShows how runtime stimuli find and resume waiting instances.
src/…/WorkflowRuntimeFeature.csShows the runtime boundary and replaceable service registrations.
src/…/WorkflowDefinition.csDefines the versioned, stored workflow definition.
src/…/IWorkflowDefinitionService.csDefines lookup and materialization into executable graphs.
src/…/IWorkflowMaterializer.csDefines the extension point for definition formats.
src/…/WorkflowsApiFeature.csShows how HTTP exposure depends on management and runtime services.
src/…/Endpoint.csShows the API boundary for queued execution of a definition.