How to: Author and manage Dapr Workflow in the .NET SDK

Learn how to author and manage Dapr Workflow using the .NET SDK

Let’s create a Dapr workflow and invoke it using the console. In the provided order processing workflow example, the console prompts provide directions on how to both purchase and restock items. In this guide, you will:

  • Deploy a .NET console application (WorkflowConsoleApp).
  • Utilize the .NET workflow SDK and API calls to start and query workflow instances.

In the .NET example project:

  • The main Program.cs file contains the setup of the app, including the registration of the workflow and workflow activities.
  • The workflow definition is found in the Workflows directory.
  • The workflow activity definitions are found in the Activities directory.

Prerequisites

Set up the environment

Clone the .NET SDK repo.

  1. git clone https://github.com/dapr/dotnet-sdk.git

From the .NET SDK root directory, navigate to the Dapr Workflow example.

  1. cd examples/Workflow

Run the application locally

To run the Dapr application, you need to start the .NET program and a Dapr sidecar. Navigate to the WorkflowConsoleApp directory.

  1. cd WorkflowConsoleApp

Start the program.

  1. dotnet run

In a new terminal, navigate again to the WorkflowConsoleApp directory and run the Dapr sidecar alongside the program.

  1. dapr run --app-id wfapp --dapr-grpc-port 4001 --dapr-http-port 3500

Dapr listens for HTTP requests at http://localhost:3500 and internal workflow gRPC requests at http://localhost:4001.

Start a workflow

To start a workflow, you have two options:

  1. Follow the directions from the console prompts.
  2. Use the workflow API and send a request to Dapr directly.

This guide focuses on the workflow API option.

Note

  • You can find the commands below in the WorkflowConsoleApp/demo.http file.
  • The body of the curl request is the purchase order information used as the input of the workflow.
  • The “12345678” in the commands represents the unique identifier for the workflow and can be replaced with any identifier of your choosing.

Run the following command to start a workflow.

  1. curl -i -X POST http://localhost:3500/v1.0-beta1/workflows/dapr/OrderProcessingWorkflow/start?instanceID=12345678 \
  2. -H "Content-Type: application/json" \
  3. -d '{"Name": "Paperclips", "TotalCost": 99.95, "Quantity": 1}'
  1. curl -i -X POST http://localhost:3500/v1.0-beta1/workflows/dapr/OrderProcessingWorkflow/start?instanceID=12345678 `
  2. -H "Content-Type: application/json" `
  3. -d '{"Name": "Paperclips", "TotalCost": 99.95, "Quantity": 1}'

If successful, you should see a response like the following:

  1. {"instanceID":"12345678"}

Send an HTTP request to get the status of the workflow that was started:

  1. curl -i -X GET http://localhost:3500/v1.0-beta1/workflows/dapr/12345678

The workflow is designed to take several seconds to complete. If the workflow hasn’t completed when you issue the HTTP request, you’ll see the following JSON response (formatted for readability) with workflow status as RUNNING:

  1. {
  2. "instanceID": "12345678",
  3. "workflowName": "OrderProcessingWorkflow",
  4. "createdAt": "2023-05-10T00:42:03.911444105Z",
  5. "lastUpdatedAt": "2023-05-10T00:42:06.142214153Z",
  6. "runtimeStatus": "RUNNING",
  7. "properties": {
  8. "dapr.workflow.custom_status": "",
  9. "dapr.workflow.input": "{\"Name\": \"Paperclips\", \"TotalCost\": 99.95, \"Quantity\": 1}"
  10. }
  11. }

Once the workflow has completed running, you should see the following output, indicating that it has reached the COMPLETED status:

  1. {
  2. "instanceID": "12345678",
  3. "workflowName": "OrderProcessingWorkflow",
  4. "createdAt": "2023-05-10T00:42:03.911444105Z",
  5. "lastUpdatedAt": "2023-05-10T00:42:18.527704176Z",
  6. "runtimeStatus": "COMPLETED",
  7. "properties": {
  8. "dapr.workflow.custom_status": "",
  9. "dapr.workflow.input": "{\"Name\": \"Paperclips\", \"TotalCost\": 99.95, \"Quantity\": 1}",
  10. "dapr.workflow.output": "{\"Processed\":true}"
  11. }
  12. }

When the workflow has completed, the stdout of the workflow app should look like:

  1. info: WorkflowConsoleApp.Activities.NotifyActivity[0]
  2. Received order 12345678 for Paperclips at $99.95
  3. info: WorkflowConsoleApp.Activities.ReserveInventoryActivity[0]
  4. Reserving inventory: 12345678, Paperclips, 1
  5. info: WorkflowConsoleApp.Activities.ProcessPaymentActivity[0]
  6. Processing payment: 12345678, 99.95, USD
  7. info: WorkflowConsoleApp.Activities.NotifyActivity[0]
  8. Order 12345678 processed successfully!

If you have Zipkin configured for Dapr locally on your machine, then you can view the workflow trace spans in the Zipkin web UI (typically at http://localhost:9411/zipkin/).

Demo

Watch this video demonstrating .NET Workflow:

Next steps