elsa-core/samples/Sample10/Program.cs

74 lines
2.9 KiB
C#
Raw Normal View History

using System;
2019-07-26 17:10:43 +00:00
using System.Data;
using System.Threading.Tasks;
using Elsa.Activities.Console.Extensions;
using Elsa.Extensions;
2019-07-25 19:06:46 +00:00
using Elsa.Models;
using Elsa.Persistence;
using Elsa.Persistence.YesSql.Extensions;
using Elsa.Runtime;
using Elsa.Services;
using Microsoft.Extensions.DependencyInjection;
2019-07-26 17:10:43 +00:00
using YesSql;
using YesSql.Provider.Sqlite;
namespace Sample10
{
// A simple demonstration of using YesSql persistence providers.
public class Program
{
public static async Task Main(string[] args)
{
var services = BuildServices();
// Invoke startup tasks.
var startupRunner = services.GetRequiredService<IStartupRunner>();
await startupRunner.StartupAsync();
// Create a workflow definition.
var registry = services.GetService<IWorkflowRegistry>();
var workflowDefinition = registry.RegisterWorkflow<HelloWorldWorkflow>();
// Mark this definition as the "latest" version.
workflowDefinition.IsLatest = true;
workflowDefinition.Version = 1;
2019-07-26 17:10:43 +00:00
using (var scope = services.CreateScope())
{
var session = services.GetRequiredService<ISession>();
2019-07-26 17:10:43 +00:00
// Persist the workflow definition.
var definitionStore = scope.ServiceProvider.GetRequiredService<IWorkflowDefinitionStore>();
await definitionStore.SaveAsync(workflowDefinition);
2019-07-26 17:10:43 +00:00
// Load the workflow definition.
workflowDefinition = await definitionStore.GetByIdAsync(workflowDefinition.Id, VersionOptions.Latest);
// Execute the workflow.
var invoker = scope.ServiceProvider.GetRequiredService<IWorkflowInvoker>();
var executionContext = await invoker.StartAsync(workflowDefinition);
2019-07-26 17:10:43 +00:00
// Persist the workflow instance.
var instanceStore = scope.ServiceProvider.GetRequiredService<IWorkflowInstanceStore>();
var workflowInstance = executionContext.Workflow.ToInstance();
await instanceStore.SaveAsync(workflowInstance);
}
}
private static IServiceProvider BuildServices()
{
return new ServiceCollection()
.AddWorkflowsCore()
.AddStartupRunner()
.AddConsoleActivities()
2019-07-26 17:10:43 +00:00
.AddYesSql(options => options
.UseSqLite(@"Data Source=c:\data\elsa.yessql.db;Cache=Shared", IsolationLevel.ReadUncommitted)
.UseDefaultIdGenerator()
.SetTablePrefix("elsa_")
2019-07-26 17:10:43 +00:00
)
.AddYesSqlWorkflowDefinitionStore()
.AddYesSqlWorkflowInstanceStore()
.BuildServiceProvider();
}
}
}