elsa-workflows / elsa-core

A .NET workflows library
https://v3.elsaworkflows.io/
MIT License
6.26k stars 1.15k forks source link

Elsa V3: Cannot use ForEach activity in coded workflows more than once per application #4440

Closed rose-a closed 1 year ago

rose-a commented 1 year ago

Hi, I have an app with two separate workflows (materialized by dependency injection, elsa.AddWorkflowsFrom<Program>()), one of which used the ForEach activity. Everything worked fine.

Then I added a new workflow to the application, which also contains a ForEach activity (using a different item type), and now on startup it throws the following exception:

System.Reflection.TargetException
  HResult=0x80131603
  Nachricht = Object does not match target type.
  Quelle = System.Private.CoreLib
  Stapelüberwachung:
   bei System.Reflection.MethodBase.ValidateInvokeTarget(Object target)
   bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   bei System.Reflection.PropertyInfo.GetValue(Object obj)
   bei Elsa.Extensions.ActivityDescriptorExtensions.<>c__DisplayClass3_0.<GetWrappedInputProperties>b__2(InputDescriptor x) in C:\Users\arose\AppData\Local\JetBrains\Shared\vAny\DecompilerCache\decompiler\3402388f89ea499c821c5cac84c004026ce00\78\26692dc8\ActivityDescriptorExtensions.cs: Zeile49
   bei System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](List`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   bei System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   bei Elsa.Extensions.ActivityDescriptorExtensions.GetWrappedInputProperties(ActivityDescriptor activityDescriptor, IActivity activity) in C:\Users\arose\AppData\Local\JetBrains\Shared\vAny\DecompilerCache\decompiler\3402388f89ea499c821c5cac84c004026ce00\78\26692dc8\ActivityDescriptorExtensions.cs: Zeile49
   bei Elsa.Workflows.Core.Services.IdentityGraphService.AssignInputOutputs(IActivity activity) in C:\Users\arose\AppData\Local\JetBrains\Shared\vAny\DecompilerCache\decompiler\3402388f89ea499c821c5cac84c004026ce00\62\e3db407a\IdentityGraphService.cs: Zeile75
   bei Elsa.Workflows.Core.Services.IdentityGraphService.AssignIdentities(ICollection`1 flattenedList) in C:\Users\arose\AppData\Local\JetBrains\Shared\vAny\DecompilerCache\decompiler\3402388f89ea499c821c5cac84c004026ce00\62\e3db407a\IdentityGraphService.cs: Zeile67
   bei Elsa.Workflows.Core.Builders.WorkflowBuilder.<BuildWorkflowAsync>d__67.MoveNext() in C:\Users\arose\AppData\Local\JetBrains\Shared\vAny\DecompilerCache\decompiler\3402388f89ea499c821c5cac84c004026ce00\f2\e0407a2b\WorkflowBuilder.cs: Zeile161
   bei Elsa.Workflows.Runtime.Providers.ClrWorkflowProvider.<BuildWorkflowAsync>d__7.MoveNext()
   bei Elsa.Workflows.Runtime.Providers.ClrWorkflowProvider.<>c__DisplayClass6_0.<<GetWorkflowsAsync>b__0>d.MoveNext()
   bei Elsa.Workflows.Runtime.Providers.ClrWorkflowProvider.<GetWorkflowsAsync>d__6.MoveNext()
   bei Open.Linq.AsyncExtensions.Extensions.<ToList>d__177`1.MoveNext()
   bei Elsa.Workflows.Runtime.Services.DefaultWorkflowDefinitionStorePopulator.<PopulateStoreAsync>d__8.MoveNext()
   bei Elsa.Workflows.Runtime.Services.DefaultRegistriesPopulator.<PopulateAsync>d__4.MoveNext()
   bei Elsa.Workflows.Runtime.HostedServices.PopulateRegistriesHostedService.<StartAsync>d__2.MoveNext()
   bei Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__12.MoveNext()
   bei Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
   bei Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
   bei Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   ...

The exception is thrown here when trying to execute x => (Input?)x.ValueGetter(activity) when processing the first ForEach activity (the one from the workflow which was working before).

As soon as I remove the ForEach from the new workflow the app runs fine again.

sfmskywalker commented 1 year ago

Hi @rose-a

Thanks for reporting the issue! Is there any possibility you can share a project that reproduces this behavior?

rose-a commented 1 year ago

I'm on it...

rose-a commented 1 year ago

There you go: https://github.com/rose-a/elsa-core/tree/v3-issue-4440-repro/src/samples/aspnet/Elsa.Samples.AspNet.Issue4440

rose-a commented 1 year ago

I suspect the issue is not just related to ForEach, but all activities with a generic argument...

sfmskywalker commented 1 year ago

Thanks for having gotten to the bottom of this @rose-a ! I'll close this issue, but feel free to reopen if you find something else that is related to this.