You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Workflow Core is a light weight workflow engine targeting .NET Standard. It supports pluggable persistence and concurrency providers to allow for multi-node clusters. See [Wiki here.](https://github.com/danielgerlag/workflow-core/wiki)
5
+
Workflow Core is a light weight workflow engine targeting .NET Standard. Think: long running processes with multiple tasks that need to track state. It supports pluggable persistence and concurrency providers to allow for multi-node clusters.
6
6
7
+
## Documentation
7
8
8
-
## Installing
9
+
See [Full Documentation here.](https://github.com/danielgerlag/workflow-core/wiki)
9
10
10
-
Install the NuGet package "WorkflowCore"
11
+
## Fluent API
11
12
12
-
```
13
-
PM> Install-Package WorkflowCore
14
-
```
15
-
16
-
## Basic Concepts
17
-
18
-
### Steps
19
-
20
-
A workflow consists of a series of connected steps. Each step produces an outcome value and subsequent steps are triggered by subscribing to a particular outcome of a preceeding step. The default outcome of *null* can be used for a basic linear workflow.
21
-
Steps are usually defined by inheriting from the StepBody abstract class and implementing the Run method. They can also be created inline while defining the workflow structure.
*The StepBody class implementations are constructed by the workflow host which first tries to use IServiceProvider from the built-in dependency injection of .NET Core, if it can't construct it with this method, it will search for a parameterless constructor*
36
-
37
-
Then we define the workflow structure by composing a chain of steps. The is done by implementing the IWorkflow interface.
38
-
39
-
```C#
40
-
publicclassHelloWorldWorkflow : IWorkflow
41
-
{
42
-
publicvoidBuild(IWorkflowBuilder<object> builder)
43
-
{
44
-
builder
45
-
.StartWith<HelloWorld>()
46
-
.Then<GoodbyeWorld>();
47
-
}
48
-
...
49
-
}
50
-
```
51
-
The *IWorkflow* interface also has a readonly Id property and readonly Version property. These are generally static and are used by the workflow host to identify a workflow definition.
52
-
53
-
You can also define your steps inline
13
+
Define your workflows with the fluent API.
54
14
55
-
```C#
56
-
publicclassHelloWorldWorkflow : IWorkflow
15
+
```c#
16
+
publicclassMyWorkflow : IWorkflow
57
17
{
58
-
publicvoidBuild(IWorkflowBuilder<object> builder)
59
-
{
18
+
publicvoidBuild(IWorkflowBuilder<MyData> builder)
19
+
{
60
20
builder
61
-
.StartWith(context=>
62
-
{
63
-
Console.WriteLine("Hello world");
64
-
returnExecutionResult.Next();
65
-
})
66
-
.Then(context=>
67
-
{
68
-
Console.WriteLine("Goodbye world");
69
-
returnExecutionResult.Next();
70
-
})
21
+
.StartWith<Task1>()
22
+
.Then<Task2>()
23
+
.Then<Task3>;
71
24
}
72
-
...
73
25
}
74
26
```
75
27
76
-
Each running workflow is persisted to the chosen persistence provider between each step, where it can be picked up at a later point in time to continue execution. The outcome result of your step can instruct the workflow host to defer further execution of the workflow until a future point in time or in response to an external event.
77
-
78
-
The first time a particular step within the workflow is called, the PersistenceData property on the context object is *null*. The ExecutionResult produced by the Run method can either cause the workflow to proceed to the next step by providing an outcome value, instruct the workflow to sleep for a defined period or simply not move the workflow forward. If no outcome value is produced, then the step becomes re-entrant by setting PersistenceData, so the workflow host will call this step again in the future buy will populate the PersistenceData with it's previous value.
79
-
80
-
For example, this step will initially run with *null* PersistenceData and put the workflow to sleep for 12 hours, while setting the PersistenceData to *new Object()*. 12 hours later, the step will be called again but context.PersistenceData will now contain the object constructed in the previous iteration, and will now produce an outcome value of *null*, causing the workflow to move forward.
Each step is intended to be a black-box, therefore they support inputs and outputs. These inputs and outputs can be mapped to a data class that defines the custom data relevant to each workflow instance.
98
-
99
-
The following sample shows how to define inputs and outputs on a step, it then shows how define a workflow with a typed class for internal data and how to map the inputs and outputs to properties on the custom data class.
.Input(step=>step.Message, data=>"The answer is "+data.Value3.ToString());
138
-
}
139
-
...
140
-
}
141
-
142
-
```
143
-
144
-
### Multiple outcomes / forking
145
-
146
-
A workflow can take a different path depending on the outcomes of preceeding steps. The following example shows a process where first a random number of 0 or 1 is generated and is the outcome of the first step. Then, depending on the outcome value, the workflow will either fork to (TaskA + TaskB) or (TaskC + TaskD)
A workflow can also wait for an external event before proceeding. In the following example, the workflow will wait for an event called *"MyEvent"* with a key of *0*. Once an external source has fired this event, the workflow will wake up and continue processing, passing the data generated by the event onto the next step.
//All workflows that have subscribed to MyEvent 0, will be passed "hello"
191
-
host.PublishEvent("MyEvent", "0", "hello");
192
-
```
193
-
194
-
### Host
195
-
196
-
The workflow host is the service responsible for executing workflows. It does this by polling the persistence provider for workflow instances that are ready to run, executes them and then passes them back to the persistence provider to by stored for the next time they are run. It is also responsible for publishing events to any workflows that may be waiting on one.
197
-
198
-
#### Setup
199
-
200
-
Use the *AddWorkflow* extension method for *IServiceCollection* to configure the workflow host upon startup of your application.
201
-
By default, it is configured with *MemoryPersistenceProvider* and *SingleNodeConcurrencyProvider* for testing purposes. You can also configure a DB persistence provider at this point.
202
-
203
-
```C#
204
-
services.AddWorkflow();
205
71
```
206
72
207
-
#### Usage
208
-
209
-
When your application starts, grab the workflow host from the built-in dependency injection framework *IServiceProvider*. Make sure you call *RegisterWorkflow*, so that the workflow host knows about all your workflows, and then call *Start()* to fire up the thread pool that executes workflows. Use the *StartWorkflow* method to initiate a new instance of a particular workflow.
By default, the WorkflowHost service will run as a single node using the built-in queue and locking providers for a single node configuration. Should you wish to run a multi-node cluster, you will need to configure an external queueing mechanism and a distributed lock manager to co-ordinate the cluster. These are the providers that are currently available.
0 commit comments