Quickstart: Workflow

Note

The workflow building block is currently in alpha.

Let’s take a look at the Dapr . In this Quickstart, you’ll create a simple console application to demonstrate Dapr’s workflow programming model and the workflow management APIs.

The order-processor console app starts and manages the lifecycle of the OrderProcessingWorkflow workflow that stores and retrieves data in a state store. The workflow consists of four workflow activities, or tasks:

  • NotifyActivity: Utilizes a logger to print out messages throughout the workflow
  • ReserveInventoryActivity: Checks the state store to ensure that there is enough inventory for the purchase
  • ProcessPaymentActivity: Processes and authorizes the payment
  • UpdateInventoryActivity: Removes the requested items from the state store and updates the store with the new remaining inventory value

In this guide, you’ll:

  • Run the order-processor application.
  • Start the workflow and watch the workflow activites/tasks execute.
  • Review the workflow logic and the workflow activities and how they’re represented in the code.

Currently, you can experience the Dapr Workflow using the .NET SDK.

For this example, you will need:

Clone the .

In a new terminal window, navigate to the order-processor directory:

  1. cd workflows/csharp/sdk/order-processor

In the terminal, start the order processor app alongside a Dapr sidecar:

This starts the order-processor app with unique workflow ID and runs the workflow activities.

Expected output:

  1. == APP == Starting workflow 6d2abcc9 purchasing 10 Cars
  2. == APP == info: Microsoft.DurableTask.Client.Grpc.GrpcDurableTaskClient[40]
  3. == APP == Scheduling new OrderProcessingWorkflow orchestration with instance ID '6d2abcc9' and 47 bytes of input data.
  4. == APP == info: WorkflowConsoleApp.Activities.NotifyActivity[0]
  5. == APP == Received order 6d2abcc9 for 10 Cars at $15000
  6. == APP == info: WorkflowConsoleApp.Activities.ReserveInventoryActivity[0]
  7. == APP == Reserving inventory for order 6d2abcc9 of 10 Cars
  8. == APP == info: WorkflowConsoleApp.Activities.ReserveInventoryActivity[0]
  9. == APP == There are: 100, Cars available for purchase
  10. == APP == Your workflow has started. Here is the status of the workflow: Dapr.Workflow.WorkflowState
  11. == APP == info: WorkflowConsoleApp.Activities.ProcessPaymentActivity[0]
  12. == APP == Processing payment: 6d2abcc9 for 10 Cars at $15000
  13. == APP == info: WorkflowConsoleApp.Activities.ProcessPaymentActivity[0]
  14. == APP == Payment for request ID '6d2abcc9' processed successfully
  15. == APP == info: WorkflowConsoleApp.Activities.UpdateInventoryActivity[0]
  16. == APP == Checking Inventory for: Order# 6d2abcc9 for 10 Cars
  17. == APP == There are now: 90 Cars left in stock
  18. == APP == info: WorkflowConsoleApp.Activities.NotifyActivity[0]
  19. == APP == Order 6d2abcc9 has completed!
  20. == APP == Workflow Status: Completed

When you ran dapr run --app-id order-processor dotnet run:

  1. A unique order ID for the workflow is generated (in the above example, ) and the workflow is scheduled.
  2. The NotifyActivity workflow activity sends a notification saying an order for 10 cars has been received.
  3. The ReserveInventoryActivity workflow activity checks the inventory data, determines if you can supply the ordered item, and responds with the number of cars in stock.
  4. Your workflow starts and notifies you of its status.
  5. The ProcessPaymentActivity workflow activity begins processing payment for order 6d2abcc9 and confirms if successful.
  6. The UpdateInventoryActivity workflow activity updates the inventory with the current available cars after the order has been processed.
  7. The NotifyActivity workflow activity sends a notification saying that order 6d2abcc9 has completed.
  8. The workflow terminates as completed.

order-processor/Program.cs

  • The unique workflow order ID is generated
  • The workflow is scheduled
  • The workflow status is retrieved
  • The workflow and the workflow activities it invokes are registered

order-processor/Workflows/OrderProcessingWorkflow.cs

In OrderProcessingWorkflow.cs, the workflow is defined as a class with all of its associated tasks (determined by workflow activities).

  1. using Dapr.Workflow;
  2. //...
  3. class OrderProcessingWorkflow : Workflow<OrderPayload, OrderResult>
  4. {
  5. public override async Task<OrderResult> RunAsync(WorkflowContext context, OrderPayload order)
  6. {
  7. string orderId = context.InstanceId;
  8. // Notify the user that an order has come through
  9. await context.CallActivityAsync(
  10. nameof(NotifyActivity),
  11. new Notification($"Received order {orderId} for {order.Quantity} {order.Name} at ${order.TotalCost}"));
  12. string requestId = context.InstanceId;
  13. // Determine if there is enough of the item available for purchase by checking the inventory
  14. InventoryResult result = await context.CallActivityAsync<InventoryResult>(
  15. nameof(ReserveInventoryActivity),
  16. new InventoryRequest(RequestId: orderId, order.Name, order.Quantity));
  17. // If there is insufficient inventory, fail and let the user know
  18. if (!result.Success)
  19. {
  20. // End the workflow here since we don't have sufficient inventory
  21. nameof(NotifyActivity),
  22. new Notification($"Insufficient inventory for {order.Name}"));
  23. }
  24. // There is enough inventory available so the user can purchase the item(s). Process their payment
  25. await context.CallActivityAsync(
  26. nameof(ProcessPaymentActivity),
  27. new PaymentRequest(RequestId: orderId, order.Name, order.Quantity, order.TotalCost));
  28. try
  29. {
  30. // There is enough inventory available so the user can purchase the item(s). Process their payment
  31. await context.CallActivityAsync(
  32. nameof(UpdateInventoryActivity),
  33. new PaymentRequest(RequestId: orderId, order.Name, order.Quantity, order.TotalCost));
  34. }
  35. catch (TaskFailedException)
  36. {
  37. // Let them know their payment was processed
  38. await context.CallActivityAsync(
  39. nameof(NotifyActivity),
  40. new Notification($"Order {orderId} Failed! You are now getting a refund"));
  41. return new OrderResult(Processed: false);
  42. }
  43. // Let them know their payment was processed
  44. await context.CallActivityAsync(
  45. nameof(NotifyActivity),
  46. new Notification($"Order {orderId} has completed!"));
  47. // End the workflow with a success result
  48. return new OrderResult(Processed: true);
  49. }
  50. }

order-processor/Activities directory

The Activities directory holds the four workflow activities used by the workflow, defined in the following files:

  • NotifyActivity.cs
  • ReserveInventoryActivity.cs
  • ProcessPaymentActivity.cs
  • UpdateInventoryActivity.cs

Watch this video to walk through the Dapr Workflow .NET demo:

We’re continuously working to improve our Quickstart examples and value your feedback. Did you find this Quickstart helpful? Do you have suggestions for improvement?

Join the discussion in our discord channel.