elsa-core/src/samples/Elsa.Samples.Serialization/Program.cs

74 lines
2.8 KiB
C#
Raw Normal View History

2020-10-17 18:19:44 +00:00
using System;
using System.Threading.Tasks;
using Elsa.Activities.Console;
2020-10-17 14:45:20 +00:00
using Elsa.Expressions;
using Elsa.Models;
2020-10-17 18:19:44 +00:00
using Elsa.Scripting.Liquid.Services;
using Elsa.Serialization;
using Elsa.Services;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Samples.Serialization
{
class Program
{
2020-10-03 10:48:25 +00:00
static async Task Main(string[] args)
{
2020-10-03 10:48:25 +00:00
// Create a service container with Elsa services.
var services = new ServiceCollection()
.AddElsa()
.AddConsoleActivities()
.BuildServiceProvider();
2020-02-18 21:29:44 +00:00
2020-10-17 14:45:20 +00:00
// Run startup actions (not needed when registering Elsa with a Host).
var startupRunner = services.GetRequiredService<IStartupRunner>();
await startupRunner.StartupAsync();
2020-10-03 10:48:25 +00:00
2020-10-17 14:45:20 +00:00
// 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
{
Id = "activity-1",
Type = nameof(WriteLine),
Properties = new ActivityDefinitionProperties
{
[nameof(WriteLine.Text)] = new ActivityDefinitionPropertyValue
{
2020-10-17 18:19:44 +00:00
Syntax = LiquidExpressionHandler.SyntaxName,
2020-10-17 14:45:20 +00:00
Expression = "Hello World!",
Type = typeof(string)
}
}
},
}
};
2020-10-03 10:48:25 +00:00
2020-10-17 18:19:44 +00:00
// Serialize workflow definition to JSON.
var serializer = services.GetRequiredService<IJsonSerializer>();
var json = serializer.Serialize(workflowDefinition);
Console.WriteLine(json);
// Deserialize workflow definition from JSON.
var deserializedWorkflowDefinition = serializer.Deserialize<WorkflowDefinition>(json);
2020-10-17 14:45:20 +00:00
// Materialize workflow.
var materializer = services.GetRequiredService<IWorkflowBlueprintMaterializer>();
2020-10-17 18:19:44 +00:00
var workflowBlueprint = materializer.CreateWorkflowBlueprint(deserializedWorkflowDefinition);
2020-10-17 14:45:20 +00:00
// Execute workflow.
var workflowHost = services.GetRequiredService<IWorkflowHost>();
await workflowHost.RunWorkflowAsync(workflowBlueprint);
}
}
}