Created
September 11, 2020 18:45
-
-
Save sfmskywalker/f290d2ab5a5cf7c5aa33238929b02509 to your computer and use it in GitHub Desktop.
Sample implementation of a JSON file-based workflow provider
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
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Elsa.Models; | |
using Elsa.Serialization; | |
using Elsa.Services; | |
using Microsoft.Extensions.Configuration; | |
public class JsonWorkflowProvider : IWorkflowProvider | |
{ | |
private readonly IWorkflowSerializer _serializer; | |
private readonly IConfiguration _configuration; | |
public JsonWorkflowProvider(IWorkflowSerializer serializer, IConfiguration configuration) | |
{ | |
_serializer = serializer; | |
_configuration = configuration; | |
} | |
public async Task<IEnumerable<WorkflowDefinitionVersion>> GetWorkflowDefinitionsAsync(CancellationToken cancellationToken) | |
{ | |
var workflowsDirectoryPath = _configuration["WorkflowPath"]; | |
var workflowFiles = Directory.GetFiles(workflowsDirectoryPath, "*.json"); | |
var workflowDefinitionTasks = workflowFiles.Select(LoadWorkflowDefinition).ToList(); | |
await Task.WhenAll(workflowDefinitionTasks); | |
return workflowDefinitionTasks.Select(x => x.Result); | |
} | |
private async Task<WorkflowDefinitionVersion> LoadWorkflowDefinition(string path) | |
{ | |
var jsonText = await File.ReadAllTextAsync(path); | |
return _serializer.Deserialize<WorkflowDefinitionVersion>(jsonText, JsonTokenFormatter.FormatName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment