Loading Workflows from JSON
Console application
1
dotnet new console -n "ElsaConsole" -f net8.0
cd ElsaConsole
dotnet add package Elsa
dotnet add package Elsa.Testing.Shared.Integration2
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Management.Mappers;
using Elsa.Workflows.Management.Models;
using Microsoft.Extensions.DependencyInjection;
// Setup service container.
var services = new ServiceCollection();
// Add Elsa services.
services.AddElsa();
// Build service container.
var serviceProvider = services.BuildServiceProvider();
// Populate registries. This is only necessary for applications that are not using hosted services.
await serviceProvider.PopulateRegistriesAsync();
// Import a workflow from a JSON file.
var workflowJson = await File.ReadAllTextAsync("HelloWorld.json");
// Get a serializer to deserialize the workflow.
var serializer = serviceProvider.GetRequiredService<IActivitySerializer>();
// Deserialize the workflow model.
var workflowDefinitionModel = serializer.Deserialize<WorkflowDefinitionModel>(workflowJson);
// Map the model to a Workflow object.
var workflowDefinitionMapper = serviceProvider.GetRequiredService<WorkflowDefinitionMapper>();
var workflow = workflowDefinitionMapper.Map(workflowDefinitionModel);
// Get a workflow runner to run the workflow.
var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>();
// Run the workflow.
await workflowRunner.RunAsync(workflow);3
{
"id": "HelloWorld-v1",
"definitionId": "HelloWorld",
"name": "Hello World",
"isLatest": true,
"isPublished": true,
"root": {
"id": "Flowchart1",
"type": "Elsa.Flowchart",
"activities": [
{
"id": "WriteLine1",
"type": "Elsa.WriteLine",
"text": {
"typeName": "String",
"expression": {
"type": "Literal",
"value": "Hello World!"
}
}
}
]
}
}4
dotnet runHello World!Elsa Server
1
2
3
{
"id": "HelloWorld-v1",
"definitionId": "HelloWorld",
"name": "Hello World",
"isLatest": true,
"isPublished": true,
"root": {
"id": "Flowchart1",
"type": "Elsa.Flowchart",
"activities": [
{
"id": "WriteLine1",
"type": "Elsa.WriteLine",
"text": {
"typeName": "String",
"expression": {
"type": "Literal",
"value": "Hello World!"
}
}
}
]
}
}4
dotnet run --urls "https://localhost:5001"5
curl --location --request POST 'https://localhost:5001/elsa/api/workflow-definitions/HelloWorld/execute' \
--header 'Authorization: ApiKey {your-api-key}'Loading Workflows from Blob Storage
dotnet add package Elsa.WorkflowProviders.BlobStorageSummary
Last updated