Non parallel ForEach on empty collection throws exception
When a ForEach is executed non parallel on an empty collection the workflow will terminate with the following exception:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at System.SZArrayHelper.get_Item[T](Int32 index) at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index) at WorkflowCore.Primitives.Foreach.Run(IStepExecutionContext context) at WorkflowCore.Models.StepBody.RunAsync(IStepExecutionContext context) at WorkflowCore.Services.StepExecutor.ExecuteStep(IStepExecutionContext context, IStepBody body) at WorkflowCore.Services.WorkflowExecutor.ExecuteStep(WorkflowInstance workflow, WorkflowStep step, ExecutionPointer pointer, WorkflowExecutorResult wfResult, WorkflowDefinition def, CancellationToken cancellationToken) at WorkflowCore.Services.WorkflowExecutor.Execute(WorkflowInstance workflow, CancellationToken cancellationToken)
This is my workflow:
builder
.UseDefaultErrorBehavior(WorkflowErrorHandling.Terminate)
.StartWith<GetInterviewsWithoutCollectedDataStepBody>()
.Output(data => data.Interviews, step => step.Interviews)
.ForEach(data => data.Interviews, _ => false)
.Do(data => data
.StartWith<GetInterviewDataStepBody>()
.Input(step => step.InterviewId, (data, context) => ((Interview)context.Item).Id)
.Input(step => step.SurveyId, (data, context) => ((Interview)context.Item).SurveyId)
.Output(data => data.CollectedAt, step => step.CollectedAt)
.Output(data => data.InterviewData, step => step.InterviewData)
.Then<SaveInterviewDataStepBody>()
.Input(step => step.InterviewId, (data, context) => ((Interview)context.Item).Id)
.Input(step => step.CollectedAt, data => data.CollectedAt)
.Input(step => step.InterviewData, data => data.InterviewData));
data.Interviews can be an empty collection (empty array). If this is the case the .ForEach(data => data.Interviews, _ => false) will throw the System.ArgumentOutOfRangeException.
✊
FIST
BUMP
:punch:
@danielgerlag, should this issue be closed?
https://github.com/danielgerlag/workflow-core/pull/895