Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Last active June 22, 2025 08:16
Show Gist options
  • Save badsyntax/cad15c9392cf99053f0802006953ec12 to your computer and use it in GitHub Desktop.
Save badsyntax/cad15c9392cf99053f0802006953ec12 to your computer and use it in GitHub Desktop.
services.AddScoped<CommitStateHandler>();
// Add Elsa services to the container.
services.AddElsa(elsa =>
{
elsa.UseWorkflows(workflows =>
{
workflows.CommitStateHandler = sp => sp.GetRequiredService<CommitStateHandler>();
workflows.WithWorkflowExecutionPipeline(pipeline => pipeline
.Reset()
.UseEngineExceptionHandling()
.UseExceptionHandling()
.UseDefaultActivityScheduler());
workflows.UseCommitStrategies(strategies =>
{
strategies.Add("ExecutedActivityStrategy", new ExecutedActivityStrategy());
});
});
elsa.UseWorkflowRuntime(runtime =>
{
runtime.DistributedLockProvider = _ => new FileDistributedSynchronizationProvider(
new DirectoryInfo(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString(), "App_Data", "locks")));
});
});
using Azure.Storage.Blobs;
using Elsa.Extensions;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.CommitStates;
namespace ElsaConsole
{
public class ExampleCommitStates
{
public static async Task Run_Example(
IWorkflowBuilderFactory workflowBuilderFactory,
IWorkflowRunner workflowRunner)
{
var workflowBuilder = workflowBuilderFactory.CreateBuilder();
// workflowBuilder.WorkflowOptions.CommitStrategyName = "WorkflowExecutedWorkflowStrategy";
var activity1 = new Inline(() =>
{
Console.WriteLine("Inline 1");
})
{
CommitStrategy = "ExecutedActivityStrategy"
};
var activity2 = new CustomActivity()
{
CommitStrategy = "ExecutedActivityStrategy"
};
workflowBuilder.Root = new Sequence
{
Activities = [
activity1,
activity2,
],
};
var workflow = await workflowBuilder.BuildWorkflowAsync();
var output = await workflowRunner.RunAsync(workflow);
}
}
public class CustomActivity : CodeActivity<string>
{
public CustomActivity()
{
}
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
Console.WriteLine("Inline 2");
context.SetResult("Hello");
await Task.CompletedTask;
}
}
}
using Elsa.Workflows;
using Elsa.Workflows.CommitStates;
using Elsa.Workflows.Management;
using Elsa.Workflows.Runtime;
using Elsa.Workflows.Runtime.Entities;
using Elsa.Workflows.Runtime.Requests;
using Elsa.Workflows.State;
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