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-contexts

Workflow context extensions

Workflow contexts let a workflow use application data that is owned outside the workflow runtime. A provider loads that data for workflow work, makes it available to activities and scripts, and saves changes back to the application at defined points. The extension connects this provider model to workflow execution, the HTTP API, and Studio authoring.

Provider model

Context providers

A workflow context provider is a service that owns the translation between application data and a workflow context value. Its LoadAsync operation supplies a value for the current workflow execution, and its SaveAsync operation receives the value after workflow work changes it. Providers can use the generic WorkflowContextProvider<T> base to make the context type explicit. IWorkflowContextProvider defines this contract, and WorkflowContextProvider supplies the typed form.

The application registers providers as services. A workflow then selects the provider types that it needs. The selection is stored with the workflow definition under Elsa:WorkflowContextProviderTypes, so the same definition tells the runtime which external data sources are part of its context. The provider service registration and workflow selection are separate: registration makes a provider available, while selection makes it active for one workflow. WorkflowContextServiceCollectionExtensions and WorkflowBuilderExtensions define these two sides.

References: src/…/IWorkflowContextProvider.cs• L5-19 src/…/WorkflowContextProvider.cs• L7-50 src/…/WorkflowContextServiceCollectionExtensions.cs• L7-21 src/…/WorkflowBuilderExtensions.cs• L14-35

Context values

The runtime keeps loaded values in the current workflow execution context. Each value is keyed by its provider type, which keeps values from different providers separate and lets activities or expressions address the right context. Provider parameters use a provider-scoped name when workflows need to pass setup data into a load operation. TransientPropertiesExecutionContextExtensions defines these access paths.

References: src/…/TransientPropertiesExecutionContextExtensions.cs• L91-190

Execution middleware

Workflow execution lifecycle

Workflow-level middleware surrounds a workflow execution cycle with context synchronization. It loads every provider selected by the workflow before the rest of the workflow pipeline runs. It saves each provider after that pipeline returns, so external application state receives the final in-memory context value for the cycle. WorkflowContextWorkflowExecutionMiddleware provides this boundary.

When a workflow execution crosses the context boundary, the provider value moves between the application and the workflow execution context before workflow work and back after that work.

flowchart LR
    A[Application data] -->|Load| B[Workflow execution context]
    B --> C[Workflow activities]
    C --> B
    B -->|Save| A

The context middleware is part of the workflow and activity execution pipelines when the host calls UseWorkflowContexts. This means the extension can be added to an execution host without changing individual activities. The workflow must still select providers, because middleware with no selected provider has no context to load or save. WorkflowExecutionBuilderExtensions exposes the pipeline hooks.

References: src/…/WorkflowContextWorkflowExecutionMiddleware.cs• L7-38 src/…/WorkflowExecutionBuilderExtensions.cs• L9-22

Activity execution lifecycle

Activity-level middleware gives each activity control over when it needs context. An activity can load a provider before it runs and save it after it runs. This limits external reads and writes to the activities that use the data instead of forcing every activity to refresh every provider. Background activity execution bypasses these per-activity choices and synchronizes all selected providers.

The activity settings have two independent meanings:

  • Load requests a provider load before the activity.
  • Save requests a provider save after the activity.

Studio stores these settings on the activity, and the runtime reads them from the activity execution context. WorkflowContextActivityExecutionMiddleware and ActivityExtensions define the execution and activity-facing behavior.

References: src/…/WorkflowContextActivityExecutionMiddleware.cs• L8-53 src/…/ActivityExtensions.cs• L22-104

Context-setting activity

Provider parameters

The context-setting activity prepares provider input during a workflow run. It selects a provider, evaluates an optional parameter name and value, writes the value into the workflow execution context, and loads that provider so the new parameter can affect the resulting context. A missing parameter name lets the provider use its default or inferred parameter. SetWorkflowContextParameter is the workflow-facing activity.

This activity is useful when a workflow learns the key for its external data during execution. Its provider input can be a literal value or an expression, so the activity can connect earlier workflow data to a later context load. The provider remains responsible for deciding how that input changes the loaded application value.

References: src/…/SetWorkflowContextParameter.cs• L12-17 src/…/SetWorkflowContextParameter.cs• L41-115 src/…/TransientPropertiesExecutionContextExtensions.cs• L17-88

Scripting support

JavaScript context access

The JavaScript integration exposes each selected workflow context as a function in expressions. The function name uses the provider name, such as getCustomer for a provider named CustomerWorkflowContextProvider, and returns the current value held by the workflow execution context. The scripting layer also describes the provider's context type so editors and evaluators can understand the returned value.

This support is enabled only when both workflow contexts and the JavaScript expression feature are active. It therefore extends the existing JavaScript environment without making JavaScript a requirement for provider-based workflows. ConfigureJavaScriptEngine and WorkflowContextsJavaScriptFeature define this integration.

References: src/…/ConfigureJavaScriptEngine.cs• L14-72 src/…/WorkflowContextsJavaScriptFeature.cs• L12-31

Provider descriptor endpoint

Descriptor discovery

The workflow-context extension exposes the registered providers as descriptors for clients that need to author workflows. The endpoint returns a display name and provider type for each registered provider. It requires the read:workflow-context-provider-descriptors permission, so provider discovery follows the host's API access rules.

The endpoint is the contract between the server's provider registry and Studio's provider lists. Studio does not need to duplicate provider definitions locally; it asks the server for the current set and uses the returned type strings when it updates workflow definitions. ProviderTypes/List/Endpoint and IWorkflowContextProviderDescriptorsApi define the server and client sides.

References: src/…/IWorkflowContextProviderDescriptorsApi.cs• L6-18 src/…/RemoteWorkflowContextsProvider.cs• L8-29

Studio editor integration

Workflow selection editor

Studio adds a Workflow Context area to workflow definition properties. It loads provider descriptors from the remote API and presents them as a set of choices. When an engineer changes a choice, Studio writes the selected provider type strings into the workflow definition and raises the normal definition-updated event.

The selected providers form the available set for the rest of the editor. This keeps provider selection at workflow-definition level while allowing activity settings and activity inputs to refer only to providers that the workflow has enabled. WorkflowContextsEditor and WorkflowDefinitionExtensions manage this definition-level contract.

References: src/…/WorkflowContextsEditor.razor.cs• L12-54 src/…/WorkflowDefinitionExtensions.cs• L12-38 src/…/WorkflowContextProviderPicker.razor.cs• L23-39 src/…/WorkflowContextActivityTabPanel.razor.cs• L43-71

Activity context settings

Studio adds a Workflow Context tab to an activity. The tab lists only the providers selected on the workflow definition and lets the author set Load and Save for each provider. Changes are written to the activity's custom properties, where the execution middleware can read them later.

The context-setting activity uses the same provider list through a dedicated input picker. The picker accepts the workflow-context-provider-picker UI hint and writes the selected provider type as a literal expression. This keeps activity configuration aligned with the workflow's selected provider set. WorkflowContextActivityTabPanel and WorkflowContextProviderPicker provide these editor surfaces.

References: src/…/WorkflowContextActivityTabPanel.razor.cs• L75-125 src/…/WorkflowContextProviderPicker.razor.cs• L14-51 src/…/WorkflowContextProviderPickerHandler.cs• L8-24

Studio module registration

The Studio feature registers the definition widget, activity tab, remote provider service, and provider-picker handler as one remote feature. The feature is available when the server advertises Elsa.WorkflowContexts, which keeps the authoring surface tied to the server capability. Feature and ServiceCollectionExtensions assemble the module.

References: src/…/Feature.cs• L10-21 src/…/ServiceCollectionExtensions.cs• L13-24 src/…/WorkflowContextsEditorWidget.cs• L7-24 src/…/WorkflowContextActivityTab.cs• L9-28

For the wider execution lifecycle, see Workflow execution. For workflows that expose or consume HTTP requests, see Workflow HTTP integration. For the broader Studio authoring model, see Studio workflow authoring.

Change checklist

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

  • Keep the provider type stored in Elsa:WorkflowContextProviderTypes compatible between workflow definitions, runtime type resolution, the descriptor endpoint, and Studio's selected-provider values.
  • Keep the descriptor route, permission, client API path, and Studio remote provider contract aligned. A mismatch can leave the editor with no provider choices even when runtime providers are registered.
  • Keep activity custom-property names and Load/Save meanings aligned between Studio serialization and execution middleware. A change in one side can silently disable context synchronization.
  • Keep provider context type descriptions and JavaScript function names derived from the same provider naming rules. Otherwise script authoring can show a function that runtime evaluation cannot provide.

Important failure modes

TriggerConsequenceGuard
A workflow selects a provider type that is not registered or cannot be resolved.The runtime cannot load or save that context, and context-dependent work fails or has no usable value.Register the provider in the host and make its type available to the workflow runtime before publishing the workflow.
The caller lacks read:workflow-context-provider-descriptors.Studio cannot discover providers through the descriptor endpoint.Grant the permission to the authoring caller or provide an authorized API client.
An activity expects context but neither its load setting nor a background execution causes a load.The activity sees no current value for that provider.Enable Load for the provider on the activity or use a workflow-level load boundary.
An activity sets Save without Load in the current activity middleware.The current middleware checks the load flag when deciding whether to save, so the save-only choice does not persist as intended.Treat Load and Save as separate runtime contracts and verify the middleware condition when changing this behavior.
JavaScript expressions call a context getter while the JavaScript integration is not enabled.The getter is not added to the script engine.Enable both the workflow-context and JavaScript features before using context getters.

Key files

FileWhy to read it
src/…/IWorkflowContextProvider.csDefines the provider load and save contract.
src/…/WorkflowContextWorkflowExecutionMiddleware.csShows the workflow-level synchronization boundary.
src/…/WorkflowContextActivityExecutionMiddleware.csShows activity-level load and save decisions.
src/…/SetWorkflowContextParameter.csShows how a workflow supplies provider input.
src/…/ConfigureJavaScriptEngine.csShows how scripts read selected contexts.
src/…/Endpoint.csDefines provider discovery for clients.
src/…/WorkflowContextsEditor.razor.csShows definition-level provider selection.
src/…/WorkflowContextActivityTabPanel.razor.csShows activity-level context settings in Studio.
src/…/WorkflowContextProviderPicker.razor.csShows provider selection for activity inputs.
src/…/Feature.csShows how the Studio surfaces are registered.