| | | 1 | | using System.Reflection; |
| | | 2 | | using Elsa.Expressions.Helpers; |
| | | 3 | | using Elsa.Workflows.Management.Attributes; |
| | | 4 | | using Elsa.Workflows.Models; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Management.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Default parameter binding implementation for host method activities. |
| | | 11 | | /// </summary> |
| | | 12 | | public class DefaultHostMethodParameterValueProvider : IHostMethodParameterValueProvider |
| | | 13 | | { |
| | | 14 | | public ValueTask<HostMethodParameterValueProviderResult> GetValueAsync(HostMethodParameterValueProviderContext conte |
| | | 15 | | { |
| | 0 | 16 | | var parameter = context.Parameter; |
| | | 17 | | |
| | | 18 | | // Provided by runtime. |
| | 0 | 19 | | if (parameter.ParameterType == typeof(CancellationToken)) |
| | 0 | 20 | | return ValueTask.FromResult(HostMethodParameterValueProviderResult.HandledValue(context.CancellationToken)); |
| | | 21 | | |
| | 0 | 22 | | if (parameter.ParameterType == typeof(ActivityExecutionContext)) |
| | 0 | 23 | | return ValueTask.FromResult(HostMethodParameterValueProviderResult.HandledValue(context.ActivityExecutionCon |
| | | 24 | | |
| | | 25 | | // Resolve from DI if explicitly requested. |
| | 0 | 26 | | if (parameter.GetCustomAttribute<FromServicesAttribute>() != null) |
| | | 27 | | { |
| | 0 | 28 | | var service = context.ServiceProvider.GetRequiredService(parameter.ParameterType); |
| | 0 | 29 | | return ValueTask.FromResult(HostMethodParameterValueProviderResult.HandledValue(service)); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | // Resolve from workflow inputs (default). |
| | 0 | 33 | | var inputDescriptor = context.InputDescriptors.FirstOrDefault(x => string.Equals(x.Name, parameter.Name, StringC |
| | 0 | 34 | | if (inputDescriptor == null) |
| | 0 | 35 | | return ValueTask.FromResult(HostMethodParameterValueProviderResult.Unhandled); |
| | | 36 | | |
| | 0 | 37 | | var input = (Input?)inputDescriptor.ValueGetter(context.Activity); |
| | 0 | 38 | | var inputValue = input != null ? context.ActivityExecutionContext.Get(input.MemoryBlockReference()) : null; |
| | | 39 | | |
| | 0 | 40 | | if (inputValue is System.Dynamic.ExpandoObject expandoObject) |
| | 0 | 41 | | inputValue = expandoObject.ConvertTo(parameter.ParameterType); |
| | | 42 | | |
| | 0 | 43 | | return ValueTask.FromResult(HostMethodParameterValueProviderResult.HandledValue(inputValue)); |
| | | 44 | | } |
| | | 45 | | } |