|
| 1 | +# Human (User) Workflow Sample |
| 2 | + |
| 3 | +This sample demonstrates how to create workflows that require human interaction using the WorkflowCore.Users extension. |
| 4 | + |
| 5 | +## What this sample shows |
| 6 | + |
| 7 | +* **User Tasks**: How to create tasks that are assigned to specific users or groups |
| 8 | +* **User Options**: How to provide multiple choice options for users to select from |
| 9 | +* **Conditional Branching**: How to execute different workflow paths based on user choices |
| 10 | +* **Task Escalation**: How to automatically reassign tasks to different users when timeouts occur |
| 11 | +* **User Action Management**: How to retrieve open user actions and publish user responses programmatically |
| 12 | + |
| 13 | +## The Workflow |
| 14 | + |
| 15 | +```c# |
| 16 | +public class HumanWorkflow : IWorkflow |
| 17 | +{ |
| 18 | + public void Build(IWorkflowBuilder<object> builder) |
| 19 | + { |
| 20 | + builder |
| 21 | + .StartWith(context => ExecutionResult.Next()) |
| 22 | + .UserTask("Do you approve", data => @"domain\bob") |
| 23 | + .WithOption("yes", "I approve").Do(then => then |
| 24 | + .StartWith(context => Console.WriteLine("You approved")) |
| 25 | + ) |
| 26 | + .WithOption("no", "I do not approve").Do(then => then |
| 27 | + .StartWith(context => Console.WriteLine("You did not approve")) |
| 28 | + ) |
| 29 | + .WithEscalation(x => TimeSpan.FromSeconds(20), x => @"domain\frank", action => action |
| 30 | + .StartWith(context => Console.WriteLine("Escalated task")) |
| 31 | + .Then(context => Console.WriteLine("Sending notification...")) |
| 32 | + ) |
| 33 | + .Then(context => Console.WriteLine("end")); |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## How it works |
| 39 | + |
| 40 | +1. **Task Assignment**: The workflow creates a user task with the prompt "Do you approve" and assigns it to `domain\bob` |
| 41 | + |
| 42 | +2. **User Options**: Two options are provided: |
| 43 | + - "yes" with label "I approve" - executes approval workflow |
| 44 | + - "no" with label "I do not approve" - executes rejection workflow |
| 45 | + |
| 46 | +3. **Escalation**: If the task is not completed within 20 seconds, it automatically escalates to `domain\frank` and executes the escalation workflow |
| 47 | + |
| 48 | +4. **User Interaction**: The program demonstrates how to: |
| 49 | + - Get open user actions using `host.GetOpenUserActions(workflowId)` |
| 50 | + - Display options to the user |
| 51 | + - Publish user responses using `host.PublishUserAction(key, user, value)` |
| 52 | + |
| 53 | +## Key Features |
| 54 | + |
| 55 | +* **UserTask**: Creates tasks that wait for human input |
| 56 | +* **WithOption**: Defines multiple choice options with conditional workflow paths |
| 57 | +* **WithEscalation**: Automatically reassigns tasks after a timeout period |
| 58 | +* **Interactive Console**: Shows how to build a simple interface for user interaction |
| 59 | + |
| 60 | +## Dependencies |
| 61 | + |
| 62 | +This sample requires the `WorkflowCore.Users` extension package, which provides the human workflow capabilities. |
| 63 | + |
| 64 | +## Use Cases |
| 65 | + |
| 66 | +This pattern is useful for: |
| 67 | +- Approval workflows |
| 68 | +- Decision-making processes |
| 69 | +- Task assignment and escalation |
| 70 | +- Interactive business processes |
| 71 | +- Multi-step user interactions |
0 commit comments