Triggers
Starting workflows from schedules, events, and file changes
A trigger decides when a workflow runs. Every trigger implements ITrigger through the shared
TriggerBase, which provides identity, enabled/active state, activation counts, and error reporting.
Table of Contents
Built-in Triggers
| Trigger | Fires when | Key configuration |
|---|---|---|
ManualTrigger | The host (or user) starts the workflow directly; the trigger only tracks active state | Optional activation payload |
ScheduleTrigger | A fixed interval elapses | Interval (must be greater than zero) |
EventTrigger | An application event is raised | Event name and optional filter |
FileWatchTrigger | A file matching a filter is created, changed, deleted, or renamed | Path, Filter, IncludeSubdirectories |
Lifecycle
Triggers are validated, started, and stopped explicitly, and they report failures instead of throwing at you:
Validate, start, observe, dispose
var trigger = new FileWatchTrigger
{
Path = @"C:\dropbox\incoming",
Filter = "*.csv",
IncludeSubdirectories = false,
WorkflowId = "nightly"
};
var validation = await trigger.ValidateAsync(); // e.g. "Directory does not exist"
if (!validation.IsValid) { /* surface it */ }
trigger.Triggered += (s, e) => Console.WriteLine($"fired with {e.TriggerData?.Count} field(s)");
await trigger.StartAsync();
// ... later
trigger.Dispose(); // stops the trigger and releases its watcher/timer
Triggers are
IDisposable. Disposing a running trigger stops it and releases the
underlying FileSystemWatcher or background loop, so hosts that discard a trigger cannot leak operating
system handles. Disposal is idempotent and safe on an already-stopped trigger, and a stopped trigger can be restarted.
Each trigger exposes IsActive, IsEnabled, LastActivated and
ActivationCount for monitoring, plus Triggered and ErrorOccurred events. The Triggered event carries TriggerId, TriggerType and a TriggerData payload; failures arrive through ErrorOccurred with the exception and message.
TriggerBase Members
| Member | Type | Description |
|---|---|---|
Id | string | Short unique id assigned at construction. |
Name / Description | string | Display identity of the trigger. |
TriggerType | TriggerType | Manual, Scheduled, WebHook, DataChange, Event, Email, FileSystem or API. |
IsActive / IsEnabled | bool | Running state and a separate enable switch. |
WorkflowId | string | The workflow this trigger starts. |
Configuration | Dictionary<string, object> | Free-form settings, mirrored by GetConfigurationSchema(). |
LastActivated / ActivationCount | DateTime? / long | Activation telemetry. |
InitializeAsync / ValidateAsync / StartAsync / StopAsync | Task | Lifecycle, all cancellation-aware. |
Example: a schedule that feeds a workflow
var engine = new WorkflowEngine();
await engine.LoadWorkflowAsync(savedWorkflow);
using var trigger = new ScheduleTrigger { Interval = TimeSpan.FromMinutes(15) };
trigger.Triggered += async (s, e) =>
{
var result = await engine.ExecuteWorkflowAsync(savedWorkflow.Id);
Console.WriteLine($"scheduled run finished: {result.Status}");
};
await trigger.StartAsync();
Related
- Workflow Engine
- Server Execution SKU — job submission with retries and status queries