Custom Activities - NeoSOFT-Technologies/workflow-plugins GitHub Wiki

Description

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.


Hello World Activity

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:

  1. Perform some work (writing some text to the console).
  2. 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:

CustomActivity1

Publish the workflow. The workflow will look like this:

CustomActivity2

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:

CustomActivity3

⚠️ **GitHub.com Fallback** ⚠️