fix workflow context execution middleware and scrip completion (#5802)

This commit is contained in:
Mohamed Ali 2024-07-20 13:35:56 +03:00 committed by GitHub
parent c117ebaad1
commit 53cb8e75c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 14 deletions

View file

@ -18,7 +18,21 @@ public static class ActivityExtensions
/// <returns>The workflow context settings.</returns>
public static IDictionary<Type, ActivityWorkflowContextSettings> GetWorkflowContextSettings(this IActivity activity)
{
return activity.CustomProperties.GetOrAdd(ActivityWorkflowContextSettingsKey, () => new Dictionary<Type, ActivityWorkflowContextSettings>())!;
var contextSetttings = activity.CustomProperties.GetOrAdd(ActivityWorkflowContextSettingsKey, () => new Dictionary<string, ActivityWorkflowContextSettings>())!;
var result = new Dictionary<Type, ActivityWorkflowContextSettings>();
foreach(var (key,value) in contextSetttings)
{
var targetType = Type.GetType(key);
if(targetType != null)
{
result.Add(targetType, value);
}
}
return result;
}
/// <summary>

View file

@ -14,5 +14,22 @@ public static class WorkflowExtensions
/// </summary>
/// <param name="workflow">The workflow to get the provider types from.</param>
/// <returns>The workflow context provider types.</returns>
public static IEnumerable<Type> GetWorkflowContextProviderTypes(this Workflow workflow) => workflow.CustomProperties.GetOrAdd(Constants.WorkflowContextProviderTypesKey, () => new List<Type>());
public static IEnumerable<Type> GetWorkflowContextProviderTypes(this Workflow workflow)
{
var contextProviderTypes = workflow.PropertyBag.GetOrAdd(Constants.WorkflowContextProviderTypesKey, () => new List<String>());
var result = new List<Type>();
foreach (var type in contextProviderTypes)
{
var targetType = Type.GetType(type);
if (targetType != null)
{
result.Add(targetType);
}
}
return result;
}
}

View file

@ -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;
/// <summary>
/// Middleware that loads and save workflow context into the currently executing workflow using installed workflow context providers.
/// </summary>
public class WorkflowContextWorkflowExecutionMiddleware : WorkflowExecutionMiddleware
{
private readonly IServiceScopeFactory _serviceScopeFactory;
/// <inheritdoc />
public WorkflowContextWorkflowExecutionMiddleware(WorkflowMiddlewareDelegate next, IServiceScopeFactory serviceScopeFactory) : base(next)
/// <inheritdoc />
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));
/// <inheritdoc />
public override async ValueTask InvokeAsync(WorkflowExecutionContext context)
{
// Check if the workflow contains any workflow context providers.
if (!context.Workflow.CustomProperties.TryGetValue<ICollection<Type>>(Constants.WorkflowContextProviderTypesKey, out var providerTypes))
if (!context.Workflow.PropertyBag.TryGetValue<ICollection<Type>>(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)
{