diff --git a/src/modules/Elsa.Workflows.Runtime/Activities/ExecuteWorkflow.cs b/src/modules/Elsa.Workflows.Runtime/Activities/ExecuteWorkflow.cs index 4fda5e8808..0836438729 100644 --- a/src/modules/Elsa.Workflows.Runtime/Activities/ExecuteWorkflow.cs +++ b/src/modules/Elsa.Workflows.Runtime/Activities/ExecuteWorkflow.cs @@ -88,7 +88,15 @@ private async ValueTask ExecuteWorkflowAsync(ActivityExec var parentInstanceId = context.WorkflowExecutionContext.Id; var input = Input.GetOrDefault(context) ?? new Dictionary(); - var correlationId = CorrelationId.GetOrDefault(context); + // propagate inout from parent workflow to child workflow + if (input.Count == 0 && context.WorkflowExecutionContext.Input != null) + { + input = context.WorkflowExecutionContext.Input; + } + + // propagate correlation id from parent workflow to child workflow + var correlationId = context.WorkflowExecutionContext.CorrelationId; + var workflowInvoker = context.GetRequiredService(); var identityGenerator = context.GetRequiredService(); var properties = new Dictionary @@ -117,7 +125,8 @@ private async ValueTask ExecuteWorkflowAsync(ActivityExec WorkflowInstanceId = options.WorkflowInstanceId, Status = workflowResult.WorkflowState.Status, SubStatus = workflowResult.WorkflowState.SubStatus, - Output = workflowResult.WorkflowState.Output + Output = workflowResult.WorkflowState.Output, + CorrelationId = correlationId }; return info; diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/ExecuteWorkflowsTests.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/ExecuteWorkflowsTests.cs index c47025d469..bff3cc9386 100644 --- a/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/ExecuteWorkflowsTests.cs +++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/ExecuteWorkflowsTests.cs @@ -1,10 +1,14 @@ -using Elsa.Common.Models; +using Elsa.Common.Entities; +using Elsa.Common.Models; +using Elsa.Workflows.Activities; using Elsa.Workflows.ComponentTests.Abstractions; using Elsa.Workflows.ComponentTests.Fixtures; using Elsa.Workflows.ComponentTests.Scenarios.ExecuteWorkflows.Workflows; using Elsa.Workflows.Models; using Elsa.Workflows.Runtime; +using Elsa.Workflows.Runtime.Filters; using Elsa.Workflows.Runtime.Messages; +using Elsa.Workflows.Runtime.OrderDefinitions; using Microsoft.Extensions.DependencyInjection; namespace Elsa.Workflows.ComponentTests.Scenarios.ExecuteWorkflows; @@ -24,8 +28,39 @@ public async Task ExecuteWorkflow_ShouldExecuteWorkflow() var workflowClient = await _workflowRuntime.CreateClientAsync(); await workflowClient.CreateInstanceAsync(new CreateWorkflowInstanceRequest { - WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionId(MainWorkflow.DefinitionId, VersionOptions.Published) + WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionId(MainWorkflow.DefinitionId, VersionOptions.Published), + CorrelationId = "test-correlation-id", + Input = new Dictionary + { + ["Value"] = 2163 + } }); - await workflowClient.RunInstanceAsync(RunWorkflowInstanceRequest.Empty); + + var response = await workflowClient.RunInstanceAsync(RunWorkflowInstanceRequest.Empty); + + // child activity + var filter = new ActivityExecutionRecordFilter { }; + var activityExecutionStore = Scope.ServiceProvider.GetRequiredService(); + var orderBy = new ActivityExecutionRecordOrder(x => x.StartedAt, OrderDirection.Ascending); + var activityExecutionRecords = await activityExecutionStore.FindManyAsync(filter, orderBy); + + var textEntries = activityExecutionRecords + .Where(r => r.ActivityState != null && r.WorkflowInstanceId != response.WorkflowInstanceId) + .SelectMany(r => r.ActivityState.Where(x => x.Key == nameof(WriteLine.Text))) + .Select(r => r.Value) + .ToList(); + Assert.Single(textEntries); + Assert.Equal("Running subroutine on value 2163 ..., correlation id is 'test-correlation-id'.", textEntries[0]); + + // parent activity + textEntries = activityExecutionRecords + .Where(r => r.ActivityState != null && r.WorkflowInstanceId == response.WorkflowInstanceId) + .SelectMany(r => r.ActivityState.Where(x => x.Key == nameof(WriteLine.Text))) + .Select(r => r.Value) + .ToList(); + Assert.Single(textEntries); + var childWorkflowInstanceId = activityExecutionRecords.Select(r => r.WorkflowInstanceId).Where(r => r != response.WorkflowInstanceId).FirstOrDefault(); + Assert.Equal("Subroutine output: {\"WorkflowDefinitionVersionId\":null,\"WorkflowInstanceId\":\"" + childWorkflowInstanceId + + "\",\"CorrelationId\":\"test-correlation-id\",\"Status\":1,\"SubStatus\":3,\"Output\":{\"Output\":4326}}", textEntries[0]); } } \ No newline at end of file diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/Workflows/MainWorkflow.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/Workflows/MainWorkflow.cs index 754fb81dfe..2675a2f90b 100644 --- a/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/Workflows/MainWorkflow.cs +++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/Workflows/MainWorkflow.cs @@ -20,10 +20,6 @@ protected override void Build(IWorkflowBuilder builder) new ExecuteWorkflow { WorkflowDefinitionId = new(SubroutineWorkflow.DefinitionId), - Input = new(new Dictionary - { - ["Value"] = 21 - }), Result = new(workflowResult) }, new WriteLine(context => $"Subroutine output: {JsonSerializer.Serialize(workflowResult.Get(context))}") diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/Workflows/SubroutineWorkflow.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/Workflows/SubroutineWorkflow.cs index e685e2f3dc..02dced1495 100644 --- a/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/Workflows/SubroutineWorkflow.cs +++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/ExecuteWorkflows/Workflows/SubroutineWorkflow.cs @@ -19,7 +19,8 @@ protected override void Build(IWorkflowBuilder builder) { Activities = { - new WriteLine(context => $"Running subroutine on value {context.GetInput(valueInput)}..."), + new WriteLine(context => $"Running subroutine on value {context.GetInput(valueInput)} ..., " + + $"correlation id is '" + context.GetWorkflowExecutionContext().CorrelationId + "'."), new SetOutput { OutputName = new(output.Name),