From 53cb8e75c358044c916b515ea25b9e2d49df2cca Mon Sep 17 00:00:00 2001 From: Mohamed Ali Date: Sat, 20 Jul 2024 13:35:56 +0300 Subject: [PATCH] fix workflow context execution middleware and scrip completion (#5802) --- .../Extensions/ActivityExtensions.cs | 16 +++++++++++- .../Extensions/WorkflowExtensions.cs | 19 +++++++++++++- ...kflowContextWorkflowExecutionMiddleware.cs | 25 ++++++++++--------- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/modules/Elsa.WorkflowContexts/Extensions/ActivityExtensions.cs b/src/modules/Elsa.WorkflowContexts/Extensions/ActivityExtensions.cs index 8d9975a03..d5c22cc0e 100644 --- a/src/modules/Elsa.WorkflowContexts/Extensions/ActivityExtensions.cs +++ b/src/modules/Elsa.WorkflowContexts/Extensions/ActivityExtensions.cs @@ -18,7 +18,21 @@ public static class ActivityExtensions /// The workflow context settings. public static IDictionary GetWorkflowContextSettings(this IActivity activity) { - return activity.CustomProperties.GetOrAdd(ActivityWorkflowContextSettingsKey, () => new Dictionary())!; + var contextSetttings = activity.CustomProperties.GetOrAdd(ActivityWorkflowContextSettingsKey, () => new Dictionary())!; + + var result = new Dictionary(); + + foreach(var (key,value) in contextSetttings) + { + var targetType = Type.GetType(key); + + if(targetType != null) + { + result.Add(targetType, value); + } + } + + return result; } /// diff --git a/src/modules/Elsa.WorkflowContexts/Extensions/WorkflowExtensions.cs b/src/modules/Elsa.WorkflowContexts/Extensions/WorkflowExtensions.cs index 25bcbde52..1ebba29a0 100644 --- a/src/modules/Elsa.WorkflowContexts/Extensions/WorkflowExtensions.cs +++ b/src/modules/Elsa.WorkflowContexts/Extensions/WorkflowExtensions.cs @@ -14,5 +14,22 @@ public static class WorkflowExtensions /// /// The workflow to get the provider types from. /// The workflow context provider types. - public static IEnumerable GetWorkflowContextProviderTypes(this Workflow workflow) => workflow.CustomProperties.GetOrAdd(Constants.WorkflowContextProviderTypesKey, () => new List()); + public static IEnumerable GetWorkflowContextProviderTypes(this Workflow workflow) + { + var contextProviderTypes = workflow.PropertyBag.GetOrAdd(Constants.WorkflowContextProviderTypesKey, () => new List()); + + var result = new List(); + + foreach (var type in contextProviderTypes) + { + var targetType = Type.GetType(type); + + if (targetType != null) + { + result.Add(targetType); + } + } + + return result; + } } \ No newline at end of file diff --git a/src/modules/Elsa.WorkflowContexts/Middleware/WorkflowContextWorkflowExecutionMiddleware.cs b/src/modules/Elsa.WorkflowContexts/Middleware/WorkflowContextWorkflowExecutionMiddleware.cs index e06764a3e..5bb6c8e49 100644 --- a/src/modules/Elsa.WorkflowContexts/Middleware/WorkflowContextWorkflowExecutionMiddleware.cs +++ b/src/modules/Elsa.WorkflowContexts/Middleware/WorkflowContextWorkflowExecutionMiddleware.cs @@ -1,7 +1,10 @@ +using System.Text.Json; +using Elsa.Expressions.Contracts; using Elsa.Extensions; using Elsa.WorkflowContexts.Contracts; using Elsa.Workflows; using Elsa.Workflows.Pipelines.WorkflowExecution; +using Elsa.Workflows.Serialization.Converters; using Microsoft.Extensions.DependencyInjection; namespace Elsa.WorkflowContexts.Middleware; @@ -9,28 +12,26 @@ namespace Elsa.WorkflowContexts.Middleware; /// /// Middleware that loads and save workflow context into the currently executing workflow using installed workflow context providers. /// -public class WorkflowContextWorkflowExecutionMiddleware : WorkflowExecutionMiddleware -{ - private readonly IServiceScopeFactory _serviceScopeFactory; - - /// - public WorkflowContextWorkflowExecutionMiddleware(WorkflowMiddlewareDelegate next, IServiceScopeFactory serviceScopeFactory) : base(next) +/// +public class WorkflowContextWorkflowExecutionMiddleware(WorkflowMiddlewareDelegate next, IServiceScopeFactory serviceScopeFactory, IWellKnownTypeRegistry wellKnownTypeRegistry) : WorkflowExecutionMiddleware(next) +{ + private readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions { - _serviceScopeFactory = serviceScopeFactory; - } - + PropertyNameCaseInsensitive = true + }.WithConverters(new TypeJsonConverter(wellKnownTypeRegistry)); + /// public override async ValueTask InvokeAsync(WorkflowExecutionContext context) { // Check if the workflow contains any workflow context providers. - if (!context.Workflow.CustomProperties.TryGetValue>(Constants.WorkflowContextProviderTypesKey, out var providerTypes)) + if (!context.Workflow.PropertyBag.TryGetValue>(Constants.WorkflowContextProviderTypesKey, out var providerTypes, _jsonSerializerOptions)) { await Next(context); return; } // Invoke each workflow context provider. - using (var scope = _serviceScopeFactory.CreateScope()) + using (var scope = serviceScopeFactory.CreateScope()) { foreach (var providerType in providerTypes) { @@ -46,7 +47,7 @@ public class WorkflowContextWorkflowExecutionMiddleware : WorkflowExecutionMiddl await Next(context); // Invoke each workflow context provider to persists the context. - using (var scope = _serviceScopeFactory.CreateScope()) + using (var scope = serviceScopeFactory.CreateScope()) { foreach (var providerType in providerTypes) {