LogoLogo
GitHub
  • Elsa Workflows 3
  • Getting Started
    • Concepts
    • 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
  • Overview
  • Base Classes for Reusable Triggers
  • Scheduling Delayed Execution with DelayFor
  • Summary

Reusable Triggers (3.5-preview)

This page explains how to create custom trigger-based activities by reusing built-in base classes like `EventBase`, `TimerBase`, and `HttpEndpointBase`. It provides examples and guidance.

Elsa Workflows provides a streamlined way to create custom activities that leverage existing trigger infrastructure. This enables developers to build their own trigger-based activities—such as timers, delays, events, and HTTP endpoints—without dealing with low-level scheduling, event wiring, or infrastructure concerns.

Overview

New base classes introduced in Elsa make it easier to implement common types of trigger-based behavior in a clean and maintainable way. These base classes encapsulate common logic, allowing your custom activities to focus on their specific behavior:

  • EventBase<T> – for custom event-driven activities.

  • TimerBase – for interval-based triggers.

  • HttpEndpointBase – for HTTP-triggered activities.

  • Activity.DelayFor(...) – schedules delayed execution from within any custom activity.

These abstractions are ideal for implementing activities similar to Delay, Timer, Event, or HttpEndpoint, but tailored to specific domain or workflow requirements.


Base Classes for Reusable Triggers

EventBase<T>

Used to implement activities that respond to named events. The base class manages event subscription and resumption automatically.

Example

public class CustomEvent : EventBase<object>
{
    protected override string GetEventName(ExpressionExecutionContext context)
    {
        return "MyEvent"; // Name of the event this activity listens to.
    }
    
    protected override void OnEventReceived(ActivityExecutionContext context, object? eventData)
    {
        Console.WriteLine("Event received with data: " + eventData);
    }
}

Use this pattern to handle domain-specific or external system events in a workflow.


TimerBase

Provides a simple way to define recurring activities based on a time interval.

Example

public class CustomTimer : TimerBase
{
    protected override TimeSpan GetInterval(ExpressionExecutionContext context)
    {
        return TimeSpan.FromSeconds(5); // Runs every 5 seconds.
    }

    protected override void OnTimerElapsed(ActivityExecutionContext context)
    {
        Console.WriteLine("Timer elapsed");
    }
}

Useful for heartbeat-style logic or periodic polling scenarios.


HttpEndpointBase

Allows you to define HTTP endpoints that act as workflow triggers without manual routing or middleware setup.

Example

public class CustomHttpEndpoint : HttpEndpointBase
{
    protected override HttpEndpointOptions GetOptions()
    {
        return new()
        {
            Path = "my-path",
            Methods = [HttpMethods.Get]
        };
    }

    protected override async ValueTask OnHttpRequestReceivedAsync(ActivityExecutionContext context, HttpContext httpContext)
    {
        httpContext.Response.StatusCode = 200;
        await httpContext.Response.WriteAsync("Hello World", context.CancellationToken);
    }
}

Ideal for building custom webhook or API-triggered workflows.


Scheduling Delayed Execution with DelayFor

Any custom activity can now schedule delayed continuation using context.DelayFor(...). This avoids the need for manually creating timer logic or separate trigger activities.

Example

public class CustomDelay : Activity
{
    protected override ValueTask ExecuteAsync(ActivityExecutionContext context)
    {
        context.DelayFor(TimeSpan.FromSeconds(5), OnDelayElapsedAsync);
        return default;
    }

    private async ValueTask OnDelayElapsedAsync(ActivityExecutionContext context)
    {
        Console.WriteLine("Delay elapsed");
        await context.CompleteActivityAsync();
    }
}

This approach is useful when delay logic is part of a larger activity's behavior.


Summary

These reusable base classes and helper methods provide a consistent, composable way to implement trigger-based activities in Elsa:

  • Reusability: Leverage prebuilt infrastructure.

  • Simplicity: Avoid boilerplate code for scheduling and triggering.

  • Consistency: Aligns with Elsa's activity and workflow execution model.

Use these tools to build rich, event-driven workflows with minimal overhead, while keeping full control over the activity logic.

PreviousCustom ActivitiesNextIntroduction

Last updated 2 months ago