forked from elsa-workflows/elsa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a sample demonstrating how to save, load & execute workflow definitions.
- Loading branch information
1 parent
252b4d0
commit 208510a
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...a.Samples.LoadAndRunFromDatabaseConsole/Elsa.Samples.LoadAndRunFromDatabaseConsole.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\activities\webhooks\Elsa.Webhooks.Persistence.EntityFramework.Sqlite\Elsa.Webhooks.Persistence.EntityFramework.Sqlite.csproj" /> | ||
<ProjectReference Include="..\..\..\core\Elsa\Elsa.csproj" /> | ||
<ProjectReference Include="..\..\..\persistence\Elsa.Persistence.EntityFramework\Elsa.Persistence.EntityFramework.Sqlite\Elsa.Persistence.EntityFramework.Sqlite.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
41 changes: 41 additions & 0 deletions
41
src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Elsa; | ||
using Elsa.Models; | ||
using Elsa.Persistence; | ||
using Elsa.Persistence.EntityFramework.Core.Extensions; | ||
using Elsa.Persistence.EntityFramework.Sqlite; | ||
using Elsa.Samples.LoadAndRunFromDatabaseConsole; | ||
using Elsa.Services; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
// Create a service container with Elsa services. | ||
var services = new ServiceCollection() | ||
.AddElsa(options => options | ||
.UseEntityFrameworkPersistence(ef => ef.UseSqlite()) | ||
.AddConsoleActivities() | ||
) | ||
.BuildServiceProvider(); | ||
|
||
// Run DB migrations. | ||
await services.GetRequiredService<IStartupRunner>().StartupAsync(); | ||
|
||
// Get a workflow definition store for saving & loading workflow definitions. | ||
var workflowDefinitionStore = services.GetRequiredService<IWorkflowDefinitionStore>(); | ||
|
||
// Create a workflow to store in DB (for demo purposes). | ||
var workflowDefinition = WorkflowDefinitionBuilder.CreateDemoWorkflowDefinition(); | ||
|
||
// Add or update the workflow definition. | ||
await workflowDefinitionStore.SaveAsync(workflowDefinition); | ||
|
||
// Load the workflow definition from DB. | ||
workflowDefinition = (await workflowDefinitionStore.FindByDefinitionIdAsync(workflowDefinition.DefinitionId, VersionOptions.Published))!; | ||
|
||
// To execute a workflow definition, we need to create a workflow blueprint from it. | ||
var workflowBlueprintMaterializer = services.GetRequiredService<IWorkflowBlueprintMaterializer>(); | ||
var workflowBlueprint = await workflowBlueprintMaterializer.CreateWorkflowBlueprintAsync(workflowDefinition); | ||
|
||
// Get a workflow runner. | ||
var workflowRunner = services.GetRequiredService<IStartsWorkflow>(); | ||
|
||
// Run the workflow. | ||
await workflowRunner.StartWorkflowAsync(workflowBlueprint); |
29 changes: 29 additions & 0 deletions
29
src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/WorkflowDefinitionBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Elsa.Activities.Console; | ||
using Elsa.Models; | ||
|
||
namespace Elsa.Samples.LoadAndRunFromDatabaseConsole; | ||
|
||
public static class WorkflowDefinitionBuilder | ||
{ | ||
public static WorkflowDefinition CreateDemoWorkflowDefinition() => new() | ||
{ | ||
Id = "SampleWorkflow-Version-1", | ||
DefinitionId = "SampleWorkflow", | ||
Version = 1, | ||
IsPublished = true, | ||
IsLatest = true, | ||
PersistenceBehavior = WorkflowPersistenceBehavior.Suspended, | ||
Activities = new[] | ||
{ | ||
new ActivityDefinition | ||
{ | ||
ActivityId = "activity-1", | ||
Type = nameof(WriteLine), | ||
Properties = new List<ActivityDefinitionProperty>() | ||
{ | ||
ActivityDefinitionProperty.Liquid(nameof(WriteLine.Text), "Hello World") | ||
} | ||
}, | ||
} | ||
}; | ||
} |