elsa-core/src/modules/Elsa.CSharp/Activities/RunCSharp/RunCSharp.cs
Sipke Schoorstra 683f0ee6d8
Add Bulk Dispatch Workflow activity (#4683)
* Initial version of BulkDispatchWorkflow

* Update comments

* Refactor options models for running workflows and add Properties

* Fix resumption

* Add ability to provide arguments when evaluating expressions

* Update args syntax for JS

* Fix ProtoActor Runtime Properties mapping

* Use default constructor

* Provide completed workflow instance ID as input to BulkDispatchWorkflows

* Implement ChildFaulted port

* Switch to Publish to make Memory service bus work

* Update input evaluation and activity output retrieval

Refined the input evaluation process to only consider inputs with AutoEvaluate set to true. Added a comment to clarify the purpose of the GetActivitiesWithOutputs method. Removed the unnecessary check for AutoEvaluate from the EvaluateInputPropertyAsync method.
2023-12-06 22:32:08 +01:00

73 lines
2.6 KiB
C#

using System.Runtime.CompilerServices;
using Elsa.CSharp.Contracts;
using Elsa.CSharp.Extensions;
using Elsa.CSharp.Models;
using Elsa.Expressions.Models;
using Elsa.Extensions;
using Elsa.Workflows.Core;
using Elsa.Workflows.Core.Attributes;
using Elsa.Workflows.Core.Models;
// ReSharper disable once CheckNamespace
namespace Elsa.CSharp.Activities;
/// <summary>
/// Executes C# code.
/// </summary>
[Activity("Elsa", "Scripting", "Executes C# code", DisplayName = "Run C#")]
public class RunCSharp : CodeActivity<object?>
{
/// <inheritdoc />
public RunCSharp([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line)
{
}
/// <inheritdoc />
public RunCSharp(string script, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(source, line)
{
Script = new Input<string>(script);
}
/// <summary>
/// The script to run.
/// </summary>
[Input(
Description = "The script to run.",
UIHint = InputUIHints.CodeEditor,
OptionsProvider = typeof(RunCSharpOptionsProvider)
)]
public Input<string> Script { get; set; } = new("");
/// <summary>
/// A list of possible outcomes. Use "SetOutcome(string)" to set the outcome. Use "SetOutcomes(params string[])" to set multiple outcomes.
/// </summary>
[Input(Description = "A list of possible outcomes.", UIHint = InputUIHints.DynamicOutcomes)]
public Input<ICollection<string>> PossibleOutcomes { get; set; } = default!;
/// <inheritdoc />
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var script = context.Get(Script);
// If no script was specified, there's nothing to do.
if (string.IsNullOrWhiteSpace(script))
return;
// Get a C# evaluator.
var evaluator = context.GetRequiredService<ICSharpEvaluator>();
// Run the script.
var options = new ExpressionEvaluatorOptions();
var result = await evaluator.EvaluateAsync(script, typeof(object), context.ExpressionExecutionContext, options, context.CancellationToken);
// Set the result as output, if any.
if (result is not null)
context.Set(Result, result);
// Get the outcome or outcomes set by the script, if any. If not set, use "Done".
var outcomes = context.ExpressionExecutionContext.TransientProperties.GetValueOrDefault(OutcomeProxy.OutcomePropertiesKey, () => new[] { "Done" })!;
// Complete the activity with the outcome.
await context.CompleteActivityWithOutcomesAsync(outcomes);
}
}