| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Dynamic; |
| | | 3 | | using System.Reflection; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | using Elsa.Expressions.Helpers; |
| | | 6 | | using Elsa.Workflows.Management.Services; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Management.Activities.HostMethod; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Executes a public async method on a configured CLR type. Internal activity used by <see cref="HostMethodActivityProv |
| | | 14 | | /// </summary> |
| | | 15 | | [Browsable(false)] |
| | | 16 | | public class HostMethodActivity : Activity |
| | | 17 | | { |
| | 0 | 18 | | [JsonIgnore] internal Type HostType { get; set; } = null!; |
| | 0 | 19 | | [JsonIgnore] internal string MethodName { get; set; } = null!; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 23 | | { |
| | 0 | 24 | | var method = ResolveMethod(MethodName); |
| | 0 | 25 | | await ExecuteInternalAsync(context, method); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | private async ValueTask ResumeAsync(ActivityExecutionContext context) |
| | | 29 | | { |
| | 0 | 30 | | var metadata = context.WorkflowExecutionContext.ResumedBookmarkContext?.Bookmark.Metadata; |
| | 0 | 31 | | if (metadata != null) |
| | | 32 | | { |
| | 0 | 33 | | var callbackMethodName = metadata["HostMethodActivityResumeCallback"]; |
| | 0 | 34 | | var callbackMethod = ResolveMethod(callbackMethodName); |
| | 0 | 35 | | await ExecuteInternalAsync(context, callbackMethod); |
| | | 36 | | } |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | private async Task ExecuteInternalAsync(ActivityExecutionContext context, MethodInfo method) |
| | | 40 | | { |
| | 0 | 41 | | var cancellationToken = context.CancellationToken; |
| | 0 | 42 | | var activityDescriptor = context.ActivityDescriptor; |
| | 0 | 43 | | var inputDescriptors = activityDescriptor.Inputs.ToList(); |
| | 0 | 44 | | var serviceProvider = context.GetRequiredService<IServiceProvider>(); |
| | 0 | 45 | | var hostInstance = ActivatorUtilities.CreateInstance(serviceProvider, HostType); |
| | 0 | 46 | | var args = await BuildArgumentsAsync(method, inputDescriptors, context, serviceProvider, cancellationToken); |
| | 0 | 47 | | var currentBookmarks = context.Bookmarks.ToList(); |
| | | 48 | | |
| | 0 | 49 | | ApplyPropertyInputs(hostInstance, inputDescriptors, context); |
| | | 50 | | |
| | 0 | 51 | | var resultValue = await InvokeAndGetResultAsync(hostInstance, method, args); |
| | 0 | 52 | | SetOutput(activityDescriptor, resultValue, context); |
| | | 53 | | |
| | | 54 | | // By convention, if no bookmarks are created, complete the activity. This may change in the future when we expo |
| | 0 | 55 | | var addedBookmarks = context.Bookmarks.Except(currentBookmarks).ToList(); |
| | 0 | 56 | | if (!addedBookmarks.Any()) |
| | | 57 | | { |
| | 0 | 58 | | await context.CompleteActivityAsync(); |
| | 0 | 59 | | return; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | // If bookmarks were created, overwrite the resume callback. We need to invoke the callback provided by the host |
| | 0 | 63 | | foreach (var bookmark in addedBookmarks) |
| | | 64 | | { |
| | 0 | 65 | | var callbackMethodName = bookmark.CallbackMethodName; |
| | | 66 | | |
| | 0 | 67 | | if (string.IsNullOrWhiteSpace(callbackMethodName)) |
| | | 68 | | continue; |
| | | 69 | | |
| | 0 | 70 | | bookmark.CallbackMethodName = nameof(ResumeAsync); |
| | 0 | 71 | | var metadata = bookmark.Metadata ?? new Dictionary<string, string>(); |
| | | 72 | | |
| | 0 | 73 | | metadata["HostMethodActivityResumeCallback"] = callbackMethodName; |
| | 0 | 74 | | bookmark.Metadata = metadata; |
| | | 75 | | } |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | private MethodInfo ResolveMethod(string methodName) |
| | | 79 | | { |
| | 0 | 80 | | var method = HostType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | |
| | 0 | 81 | | if (method == null) |
| | 0 | 82 | | throw new InvalidOperationException($"Method '{methodName}' not found on type '{HostType.Name}'."); |
| | | 83 | | |
| | 0 | 84 | | return method; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | private async ValueTask<object?[]> BuildArgumentsAsync(MethodInfo method, IReadOnlyCollection<InputDescriptor> input |
| | | 88 | | { |
| | 0 | 89 | | var parameters = method.GetParameters(); |
| | 0 | 90 | | var args = new object?[parameters.Length]; |
| | | 91 | | |
| | | 92 | | // Allow multiple providers; call in order. |
| | 0 | 93 | | var providers = serviceProvider.GetServices<IHostMethodParameterValueProvider>().ToList(); |
| | 0 | 94 | | if (providers.Count == 0) |
| | 0 | 95 | | providers.Add(new DefaultHostMethodParameterValueProvider()); |
| | | 96 | | |
| | 0 | 97 | | for (var i = 0; i < parameters.Length; i++) |
| | | 98 | | { |
| | 0 | 99 | | var parameter = parameters[i]; |
| | | 100 | | |
| | 0 | 101 | | var providerContext = new HostMethodParameterValueProviderContext( |
| | 0 | 102 | | serviceProvider, |
| | 0 | 103 | | context, |
| | 0 | 104 | | inputDescriptors, |
| | 0 | 105 | | this, |
| | 0 | 106 | | parameter, |
| | 0 | 107 | | cancellationToken); |
| | | 108 | | |
| | 0 | 109 | | var handled = false; |
| | 0 | 110 | | object? value = null; |
| | | 111 | | |
| | 0 | 112 | | foreach (var provider in providers) |
| | | 113 | | { |
| | 0 | 114 | | var result = await provider.GetValueAsync(providerContext); |
| | 0 | 115 | | if (!result.Handled) |
| | | 116 | | continue; |
| | | 117 | | |
| | 0 | 118 | | handled = true; |
| | 0 | 119 | | value = result.Value; |
| | 0 | 120 | | break; |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | if (handled) |
| | | 124 | | { |
| | 0 | 125 | | args[i] = value; |
| | 0 | 126 | | continue; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | // No provider handled it: fall back to parameter default value (if any). |
| | 0 | 130 | | args[i] = parameter.HasDefaultValue ? parameter.DefaultValue : null; |
| | 0 | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | return args; |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | private void ApplyPropertyInputs(object hostInstance, IReadOnlyCollection<InputDescriptor> inputDescriptors, Activit |
| | | 137 | | { |
| | 0 | 138 | | var hostPropertyLookup = HostType.GetProperties().ToDictionary(x => x.Name, x => x); |
| | | 139 | | |
| | 0 | 140 | | foreach (var inputDescriptor in inputDescriptors) |
| | | 141 | | { |
| | 0 | 142 | | if (!hostPropertyLookup.TryGetValue(inputDescriptor.Name, out var prop) || !prop.CanWrite) |
| | | 143 | | continue; |
| | | 144 | | |
| | 0 | 145 | | var input = (Input?)inputDescriptor.ValueGetter(this); |
| | 0 | 146 | | var inputValue = input != null ? context.Get(input.MemoryBlockReference()) : null; |
| | 0 | 147 | | inputValue = ConvertIfNeeded(inputValue, prop.PropertyType); |
| | | 148 | | |
| | 0 | 149 | | prop.SetValue(hostInstance, inputValue); |
| | | 150 | | } |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | private async Task<object?> InvokeAndGetResultAsync(object hostInstance, MethodInfo method, object?[] args) |
| | | 154 | | { |
| | 0 | 155 | | var invocationResult = method.Invoke(hostInstance, args); |
| | | 156 | | |
| | | 157 | | // Synchronous methods. |
| | 0 | 158 | | if (invocationResult is not Task task) |
| | | 159 | | { |
| | 0 | 160 | | return method.ReturnType == typeof(void) ? null : invocationResult; |
| | | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | await task; |
| | | 164 | | |
| | | 165 | | // Task<T>. |
| | 0 | 166 | | if (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)) |
| | | 167 | | { |
| | 0 | 168 | | var resultProperty = task.GetType().GetProperty("Result"); |
| | 0 | 169 | | return resultProperty?.GetValue(task); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | // Task. |
| | 0 | 173 | | return null; |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | private void SetOutput(ActivityDescriptor activityDescriptor, object? resultValue, ActivityExecutionContext context) |
| | | 177 | | { |
| | 0 | 178 | | var outputDescriptor = activityDescriptor.Outputs.SingleOrDefault(); |
| | 0 | 179 | | if (outputDescriptor == null) |
| | 0 | 180 | | return; |
| | | 181 | | |
| | 0 | 182 | | var output = (Output?)outputDescriptor.ValueGetter(this); |
| | 0 | 183 | | context.Set(output, resultValue, outputDescriptor.Name); |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | private static object? ConvertIfNeeded(object? value, Type targetType) |
| | | 187 | | { |
| | 0 | 188 | | if (value is ExpandoObject expandoObject) |
| | 0 | 189 | | return expandoObject.ConvertTo(targetType); |
| | | 190 | | |
| | 0 | 191 | | return value; |
| | | 192 | | } |
| | | 193 | | } |