Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Last active June 22, 2025 06:42
Show Gist options
  • Save badsyntax/8689341bd75e5688d06b875c6dc9bdbb to your computer and use it in GitHub Desktop.
Save badsyntax/8689341bd75e5688d06b875c6dc9bdbb to your computer and use it in GitHub Desktop.
Elsa 3.4.0 configuration for running short lived workflows that don't require any persistence
var services = new ServiceCollection();
services.AddScoped<CommitStateHandler>();
services.AddElsa(elsa =>
{
elsa.UseWorkflows(workflows =>
{
workflows.CommitStateHandler = sp => sp.GetRequiredService<CommitStateHandler>();
workflows.WithWorkflowExecutionPipeline(pipeline => pipeline
.Reset()
.UseEngineExceptionHandling()
.UseExceptionHandling()
.UseDefaultActivityScheduler());
});
elsa.UseWorkflowRuntime(runtime =>
{
runtime.DistributedLockProvider = _ => new FileDistributedSynchronizationProvider(
new DirectoryInfo(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString(), "App_Data", "locks")));
});
});
public class CommitStateHandler(
IWorkflowInstanceManager workflowInstanceManager,
IBookmarksPersister bookmarkPersister,
IVariablePersistenceManager variablePersistenceManager,
ILogRecordSink<ActivityExecutionRecord> activityExecutionLogRecordSink,
ILogRecordSink<WorkflowExecutionLogRecord> workflowExecutionLogRecordSink) : ICommitStateHandler
{
public async Task CommitAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken cancellationToken = default)
{
var workflowState = workflowInstanceManager.ExtractWorkflowState(workflowExecutionContext);
await CommitAsync(workflowExecutionContext, workflowState, cancellationToken);
}
public async Task CommitAsync(WorkflowExecutionContext workflowExecutionContext, WorkflowState workflowState, CancellationToken cancellationToken = default)
{
// var updateBookmarksRequest = new UpdateBookmarksRequest(workflowExecutionContext, workflowExecutionContext.BookmarksDiff, workflowExecutionContext.CorrelationId);
// await bookmarkPersister.PersistBookmarksAsync(updateBookmarksRequest);
// await activityExecutionLogRecordSink.PersistExecutionLogsAsync(workflowExecutionContext, cancellationToken);
// await workflowExecutionLogRecordSink.PersistExecutionLogsAsync(workflowExecutionContext, cancellationToken);
// await variablePersistenceManager.SaveVariablesAsync(workflowExecutionContext);
await workflowInstanceManager.SaveAsync(workflowState, cancellationToken);
workflowExecutionContext.ExecutionLog.Clear();
workflowExecutionContext.ClearCompletedActivityExecutionContexts();
await workflowExecutionContext.ExecuteDeferredTasksAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment