| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text; |
| | | 3 | | using Elsa.Expressions.CSharp.Contracts; |
| | | 4 | | using Elsa.Expressions.CSharp.Models; |
| | | 5 | | using Elsa.Expressions.CSharp.Notifications; |
| | | 6 | | using Elsa.Expressions.CSharp.Options; |
| | | 7 | | using Elsa.Expressions.Models; |
| | | 8 | | using Elsa.Mediator.Contracts; |
| | | 9 | | using Microsoft.CodeAnalysis; |
| | | 10 | | using Microsoft.CodeAnalysis.CSharp.Scripting; |
| | | 11 | | using Microsoft.CodeAnalysis.Scripting; |
| | | 12 | | using Microsoft.Extensions.Caching.Memory; |
| | | 13 | | using Microsoft.Extensions.Options; |
| | | 14 | | |
| | | 15 | | namespace Elsa.Expressions.CSharp.Services; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// A C# expression evaluator using Roslyn. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// Initializes a new instance of the <see cref="CSharpEvaluator"/> class. |
| | | 22 | | /// </remarks> |
| | 0 | 23 | | public class CSharpEvaluator(INotificationSender notificationSender, IOptions<CSharpOptions> scriptOptions, IMemoryCache |
| | | 24 | | { |
| | 0 | 25 | | private readonly CSharpOptions _csharpOptions = scriptOptions.Value; |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public async Task<object?> EvaluateAsync( |
| | | 29 | | string expression, |
| | | 30 | | Type returnType, |
| | | 31 | | ExpressionExecutionContext context, |
| | | 32 | | ExpressionEvaluatorOptions options, |
| | | 33 | | Func<ScriptOptions, ScriptOptions>? configureScriptOptions = default, |
| | | 34 | | Func<Script<object>, Script<object>>? configureScript = default, |
| | | 35 | | CancellationToken cancellationToken = default) |
| | | 36 | | { |
| | 0 | 37 | | var scriptOptions = ScriptOptions.Default.WithOptimizationLevel(OptimizationLevel.Release); |
| | | 38 | | |
| | 0 | 39 | | if (configureScriptOptions != null) |
| | 0 | 40 | | scriptOptions = configureScriptOptions(scriptOptions); |
| | | 41 | | |
| | 0 | 42 | | var globals = new Globals(context, options.Arguments); |
| | 0 | 43 | | var script = CSharpScript.Create("", scriptOptions, typeof(Globals)); |
| | | 44 | | |
| | 0 | 45 | | if (configureScript != null) |
| | 0 | 46 | | script = configureScript(script); |
| | | 47 | | |
| | 0 | 48 | | var notification = new EvaluatingCSharp(options, script, scriptOptions, context); |
| | 0 | 49 | | await notificationSender.SendAsync(notification, cancellationToken); |
| | 0 | 50 | | scriptOptions = notification.ScriptOptions; |
| | 0 | 51 | | script = notification.Script.ContinueWith(expression, scriptOptions); |
| | 0 | 52 | | var runner = GetCompiledScript(script); |
| | 0 | 53 | | return await runner(globals, cancellationToken: cancellationToken); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | private ScriptRunner<object> GetCompiledScript(Script<object> script) |
| | | 57 | | { |
| | 0 | 58 | | var cacheKey = "csharp:script:" + Hash(script); |
| | | 59 | | |
| | 0 | 60 | | return memoryCache.GetOrCreate(cacheKey, entry => |
| | 0 | 61 | | { |
| | 0 | 62 | | if (_csharpOptions.ScriptCacheTimeout.HasValue) |
| | 0 | 63 | | entry.SetSlidingExpiration(_csharpOptions.ScriptCacheTimeout.Value); |
| | 0 | 64 | | |
| | 0 | 65 | | return script.CreateDelegate(); |
| | 0 | 66 | | })!; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | private static string Hash(Script<object> script) |
| | | 70 | | { |
| | 0 | 71 | | var ms = new MemoryStream(); |
| | 0 | 72 | | using (var sw = new StreamWriter(ms, Encoding.UTF8)) |
| | | 73 | | { |
| | 0 | 74 | | for (Script current = script; current != null; current = current.Previous) |
| | | 75 | | { |
| | 0 | 76 | | sw.WriteLine(current.Code); |
| | | 77 | | } |
| | 0 | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | if (!ms.TryGetBuffer(out var segment)) |
| | | 81 | | { |
| | 0 | 82 | | segment = ms.ToArray(); |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | var hash = SHA256.HashData(segment.AsSpan()); |
| | 0 | 86 | | return Convert.ToBase64String(hash); |
| | | 87 | | } |
| | | 88 | | } |