Custom Activities - NeoSOFT-Technologies/workflow-plugins GitHub Wiki
A custom workflow activity is a step created and used within the workflow engine. It allows you to add complex processing steps to your configurable workflows. If you want to achieve a business task within a workflow or dialog then custom workflow is an option.
One of the great features of a custom workflow activity is that it can take input parameters and generate output parameters.
We will take a look at a simple activity called Hello World Activity that writes the text Hello World to standard out.
Creating a custom activity typically involves the following steps:
Create a new Activities folder in the Workflow project. Then create a new class called HelloWorld.
using Elsa.Services;
using Elsa.Attributes;
namespace ElsaWorkflowServer.Activities
{
public class HelloWorld : Activity
{
protected override IActivityExecutionResult OnExecute()
{
Console.WriteLine("Hello World!");
return Done();
}
}
}
The above code has the absolute minimum requirement to implement an activity by implementing the IActivity interface which is inherited from the Activity base class.
The activity does two things:
- Perform some work (writing some text to the console).
- Return an IActivityExecutionResult - an OutcomeResult in this example.
In order for this activity to be easier to understand by users of the workflow designer, it is recommended to provide a Category, DisplayName and a Description as metadata by applying the ActivityAttribute on the class:
[Activity(
Category = "Custom",
DisplayName = "Hello World Message",
Description = "Write Hello World text to standard out."
)]
The HelloWorld activity class will look like:
using Elsa.Services;
using Elsa.Attributes;
using System;
using Elsa.ActivityResults;
namespace ElsaWorkflowServer.Activities
{
[Activity(
Category = "Custom",
DisplayName = "Hello World Message",
Description = "Write Hello World text to standard out."
)]
public class HelloWorld : Activity
{
protected override IActivityExecutionResult OnExecute()
{
Console.WriteLine("Hello World!");
return Done();
}
}
}
Register Activity: Before we can actually use the activity on a workflow, we need to register it with Elsa. To do that, update the ConfigureServices class of Startup.cs file and add the following code right after the .AddHttpActivities() invocation:
.AddActivity<HelloWorld>()
Now, start the web application and create a new workflow by adding HTTP Endpoint activity with path /custom-activity. Then add a new activity named Hello World Message:
Publish the workflow. The workflow will look like this:
Open Postman and execute the following request to invoke the workflow using the Elsa API:
https://localhost:5001/custom-activity
The response should look like this in Terminal: