Skip to content

Instantly share code, notes, and snippets.

@rzvdaniel
Last active February 21, 2021 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rzvdaniel/76ff2a1aee4978444d8013c0fce0e33c to your computer and use it in GitHub Desktop.
Save rzvdaniel/76ff2a1aee4978444d8013c0fce0e33c to your computer and use it in GitHub Desktop.
Elsa Workflows Declarative Example
// Create a service container with Elsa services.
var services = new ServiceCollection()
.AddElsa()
// For production use.
.UseYesSqlPersistence()
// Or use any of the other supported persistence providers such as EF Core or MongoDB:
// .UseEntityFrameworkPersistence(ef => ef.UseSqlite())
// .UseMongoDbPersistence()
.BuildServiceProvider();
// Run startup actions (not needed when registering Elsa with a Host).
var startupRunner = services.GetRequiredService<IStartupRunner>();
await startupRunner.StartupAsync();
// Define a workflow.
var workflowDefinition = new WorkflowDefinition
{
WorkflowDefinitionId = "SampleWorkflow",
WorkflowDefinitionVersionId = "1",
Version = 1,
IsPublished = true,
IsLatest = true,
IsEnabled = true,
PersistenceBehavior = WorkflowPersistenceBehavior.Suspended,
Activities = new[]
{
new ActivityDefinition
{
ActivityId = "activity-1",
Type = nameof(WriteLine),
Properties = new ActivityDefinitionProperties
{
[nameof(WriteLine.Text)] = new ActivityDefinitionPropertyValue
{
Syntax = "Literal",
Expression = "Hello World!",
Type = typeof(string)
}
}
},
}
};
// Serialize workflow definition to JSON.
var serializer = services.GetRequiredService<IContentSerializer>();
var json = serializer.Serialize(workflowDefinition);
// Deserialize workflow definition from JSON.
var deserializedWorkflowDefinition = serializer.Deserialize<WorkflowDefinition>(json);
// Materialize workflow.
var materializer = services.GetRequiredService<IWorkflowBlueprintMaterializer>();
var workflowBlueprint = materializer.CreateWorkflowBlueprint(deserializedWorkflowDefinition);
// Execute workflow.
var workflowRunner = services.GetRequiredService<IWorkflowRunner>();
await workflowRunner.RunWorkflowAsync(workflowBlueprint);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment