Last active
February 21, 2021 00:20
-
-
Save rzvdaniel/76ff2a1aee4978444d8013c0fce0e33c to your computer and use it in GitHub Desktop.
Elsa Workflows Declarative Example
This file contains hidden or 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
// 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