Elsa Core
Elsa Core 之 自定义Activity
[Activity(
Category = "Demo",
DisplayName = "Write Line",
Description = "Write text to standard out.",
Outcomes = new[] { OutcomeNames.Done , "Success", "Failed" }
)]
public class WriteLine : Activity
{
[ActivityInput(Hint = "The message to write.")]
public string Message { get; set; }
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
{
var success = DoSomething();
var outcomeName = success ? "Success" : "Failed";
return Outcome(outcomeName);
}
}
异步执行
public class SayHelloWorld : Activity
{
private readonly TextWriter _writer;
public SayHelloWorld(TextWriter writer)
{
_writer = writer;
}
protected override async ValueTask<IActivityExecutionResult> OnExecuteAsync(ActivityExecutionContext context)
{
await _writer.WriteLineAsync("Hello World!");
return Done();
}
}
注入
public class WriteLine : Activity
{
private readonly TextWriter _writer;
public WriteLine(TextWriter writer)
{
_writer = writer;
}
[ActivityInput(Hint = "The message to write.")]
public string Message { get; set; }
protected override async ValueTask<IActivityExecutionResult> OnExecuteAsync(ActivityExecutionContext context)
{
await _writer.WriteLineAsync(Message);
return Done();
}
}
中断和继续
public class ReadLine : Activity
{
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
{
// Instruct the workflow runner to suspend the workflow.
return Suspend();
}
protected override IActivityExecutionResult OnResume(ActivityExecutionContext context)
{
// Read received input.
var receivedInput = context.GetInput<string>();
// Store received input into activity output.
context.Output = receivedInput;
// Instruct workflow runner that we're done.
return Done();
}
}
初始化
services.AddElsa(elsaOptions => elsaOptions.AddActivity<SayHelloWorld>());
也可以这样
services.AddElsa(elsaOptions => elsaOptions.AddActivitiesFrom<TMarkerType>());
参考:
Writing Custom Activities · ELSA (elsa-workflows.github.io)