# Single Workflow Console Sample This sample demonstrates how to run a workflow as a durable orchestration from a console application using the Durable Task Framework. It showcases the **durability** aspect - if the process crashes mid-execution, the workflow can be resumed without re-executing completed activities. ## Overview The sample implements an order cancellation workflow with three executors, each with artificial delays to simulate real-world operations: 1. **OrderLookup** (2 seconds) - Looks up an order by its ID 2. **OrderCancel** (5 seconds) - Marks the order as cancelled 3. **SendEmail** (1 second) - Sends a cancellation confirmation email ## Durability Demonstration The key feature of Durable Task Framework is **durability**: - **Activity results are persisted**: When an activity completes, its result is saved - **Orchestrations are replayed**: On restart, the orchestration replays from the beginning - **Completed activities are skipped**: The framework uses cached results for completed activities - **Failed activities are retried**: If an activity was interrupted, it runs again - **Automatic resume**: When the worker starts, it automatically picks up any pending work! ### Try It Yourself 1. Start the application and enter an order ID (e.g., `12345`) 2. Stop the app (Ctrl+C or stop debugging) during the `OrderCancel` activity (5 seconds) 3. Restart the application 4. **Watch for automatic resume!** The worker automatically picks up the interrupted workflow 5. Observe that `OrderLookup` is NOT re-executed (its result was cached) 6. `OrderCancel` restarts from the beginning (it didn't complete) 7. `SendEmail` runs after `OrderCancel` completes The durability is completely automatic - no manual intervention needed! ## Workflow Flow ``` User Input (Order ID) ? ? ??????????????????? ? OrderLookup ? ? 2 second delay (database lookup) ? (2 seconds) ? ??????????????????? ? ? ??????????????????? ? OrderCancel ? ? 5 second delay - TRY INTERRUPTING HERE! ? (5 seconds) ? ??????????????????? ? ? ??????????????????? ? SendEmail ? ? 1 second delay (email sending) ? (1 second) ? ??????????????????? ? ? Result ``` ## Key Concepts Demonstrated - **ConfigureDurableWorkflows** - Simplified API for registering workflows - **DurableExecution.RunAsync** - Start a new workflow (similar to InProcessExecution) - **DurableRun** - Handle to monitor and interact with a running workflow - **Automatic Resume** - Interrupted workflows continue automatically on restart ## Environment Setup See the [README.md](../README.md) file in the parent directory for more information on how to configure the environment, including how to install and run common sample dependencies. ## Running the Sample With the environment setup, you can run the sample: ```bash cd dotnet/samples/DurableAgents/ConsoleApps/08_SingleWorkflow dotnet run --framework net10.0 ``` ### Sample Session ```text ?????????????????????????????????????????????????????????????????????? ? Durable Workflow Console Sample ? ?????????????????????????????????????????????????????????????????????? ? This sample demonstrates durability in workflows. ? ? Workflow: OrderLookup (2s) -> OrderCancel (5s) -> SendEmail (1s) ? ?????????????????????????????????????????????????????????????????????? ?? TIP: Stop the app during OrderCancel (5 seconds) to test durability! Restart the app - it will automatically resume from where it left off. ? Checking for pending workflows... Enter an order ID to start a new workflow (or 'exit' to quit): Order ID: 12345 Starting workflow for order '12345'... Instance ID: abc123-def456-... ??????????????????????????????????????????????????????????????????? ? [Activity] OrderLookup: Starting lookup for order '12345' ? [Activity] OrderLookup: Found order '12345' for customer 'Jerry' ??????????????????????????????????????????????????????????????????? ??????????????????????????????????????????????????????????????????? ? [Activity] OrderCancel: Starting cancellation for order '12345' ? [Activity] OrderCancel: ?? This takes 5 seconds - try Ctrl+C! ? [Activity] OrderCancel: Processing... 1/5 seconds ? [Activity] OrderCancel: Processing... 2/5 seconds ^C <-- User stops the app here [After restart...] ? Checking for pending workflows... ??????????????????????????????????????????????????????????????????? ? [Activity] OrderCancel: Starting cancellation for order '12345' <-- Auto-resumed! ? [Activity] OrderCancel: ?? This takes 5 seconds - try Ctrl+C! ? [Activity] OrderCancel: Processing... 1/5 seconds ... ? [Activity] OrderCancel: ? Order '12345' has been cancelled ??????????????????????????????????????????????????????????????????? ??????????????????????????????????????????????????????????????????? ? [Activity] SendEmail: Sending email to 'jerry@example.com'... ? [Activity] SendEmail: ? Email sent successfully! ??????????????????????????????????????????????????????????????????? Enter an order ID to start a new workflow (or 'exit' to quit): Order ID: _ ``` Notice that when resumed: - `OrderLookup` was **NOT re-executed** (result was cached by Durable Task) - `OrderCancel` **restarted automatically** (it was interrupted before completing) - `SendEmail` ran normally after `OrderCancel` completed ## Viewing Workflow State You can view the state of the workflow in the Durable Task Scheduler dashboard: 1. Open your browser and navigate to `http://localhost:8082` 2. In the dashboard, you can view the state of the orchestration, including activity history and current state ## Related Samples - [01_SingleAgent](../01_SingleAgent) - Single agent console sample - [02_AgentOrchestration_Chaining](../02_AgentOrchestration_Chaining) - Agent chaining with durable orchestration - [05_AgentOrchestration_HITL](../05_AgentOrchestration_HITL) - Human-in-the-loop orchestration - [09_Workflow](../../AzureFunctions/09_Workflow) - Azure Functions version of workflow hosting