|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "Workflow management operations with DaprWorkflowClient" |
| 4 | +linkTitle: "Workflow management operations" |
| 5 | +weight: 2000 |
| 6 | +description: Learn how to use the `DaprWorkflowClient` to manage workflows |
| 7 | +--- |
| 8 | + |
| 9 | +## Workflow management operations with `DaprWorkflowClient` |
| 10 | + |
| 11 | +The `DaprWorkflowClient` class provides methods to manage workflow instances. Below are the operations you can perform using the `DaprWorkflowClient`. |
| 12 | + |
| 13 | +### Schedule a new workflow instance |
| 14 | + |
| 15 | +To start a new workflow instance, use the `ScheduleNewWorkflowAsync` method. This method requires the workflow type name and an input required by the workflow. The workflow `instancedId` is an optional argument; if not provided, a new GUID is generated by the `DaprWorkflowClient`. The final optional argument is a `startTime` of type `DateTimeOffset` which can be used to define when the workflow instance should start. The method returns the `instanceId` of the scheduled workflow which is used for other workflow management operations. |
| 16 | + |
| 17 | +```csharp |
| 18 | +var instanceId = $"order-workflow-{Guid.NewGuid().ToString()[..8]}"; |
| 19 | +var input = new Order("Paperclips", 1000, 9.95); |
| 20 | +await daprWorkflowClient.ScheduleNewWorkflowAsync( |
| 21 | + nameof(OrderProcessingWorkflow), |
| 22 | + instanceId, |
| 23 | + input); |
| 24 | +``` |
| 25 | + |
| 26 | +### Retrieve the status of a workflow instance |
| 27 | + |
| 28 | +To get the current status of a workflow instance, use the `GetWorkflowStateAsync` method. This method requires the instance ID of the workflow and returns a `WorkflowStatus` object containing details about the workflow's current state. |
| 29 | + |
| 30 | +```csharp |
| 31 | +var workflowStatus = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); |
| 32 | +``` |
| 33 | + |
| 34 | +### Raise an event to a running workflow instance |
| 35 | + |
| 36 | +To send an event to a running workflow instance that is waiting for an external event, use the `RaiseEventAsync` method. This method requires the instance ID of the workflow, the name of the event, and optionally the event payload. |
| 37 | + |
| 38 | +```csharp |
| 39 | +await daprWorkflowClient.RaiseEventAsync(instanceId, "Approval", true); |
| 40 | +``` |
| 41 | + |
| 42 | +### Suspend a running workflow instance |
| 43 | + |
| 44 | +A running workflow instance can be paused using the `SuspendWorkflowAsync` method. This method requires the instance ID of the workflow. You can optionally provide a reason for suspending the workflow. |
| 45 | + |
| 46 | +```csharp |
| 47 | +await daprWorkflowClient.SuspendWorkflowAsync(instanceId); |
| 48 | +``` |
| 49 | + |
| 50 | +### Resume a suspended workflow instance |
| 51 | + |
| 52 | +A suspended workflow instance can be resumed using the `ResumeWorkflowAsync` method. This method requires the instance ID of the workflow. You can optionally provide a reason for resuming the workflow. |
| 53 | + |
| 54 | +```csharp |
| 55 | +await daprWorkflowClient.ResumeWorkflowAsync(instanceId); |
| 56 | +``` |
| 57 | + |
| 58 | +### Terminate a workflow instance |
| 59 | + |
| 60 | +To terminate a workflow instance, use the `TerminateWorkflowAsync` method. This method requires the instance ID of the workflow. You can optionally provide an `output` argument of type `string`. Terminating a workflow instance will also terminal all child workflow instances but it has no impact on in-flight activity executions. |
| 61 | + |
| 62 | +```csharp |
| 63 | +await daprWorkflowClient.TerminateWorkflowAsync(instanceId); |
| 64 | +``` |
| 65 | + |
| 66 | +### Purge a workflow instance |
| 67 | + |
| 68 | +To remove the workflow instance history from the Dapr Workflow state store, use the `PurgeWorkflowAsync` method. This method requires the instance ID of the workflow. Only completed, failed, or terminated workflow instances can be purged. |
| 69 | + |
| 70 | +```csharp |
| 71 | +await daprWorkflowClient.PurgeWorkflowAsync(instanceId); |
| 72 | +``` |
| 73 | + |
| 74 | +## Next steps |
| 75 | + |
| 76 | +- [Learn how to author workflows and activities]({{% ref howto-author-workflow.md %}}) |
| 77 | +- [Try the Dapr Workflow quickstart]({{% ref workflow-quickstart.md %}}) |
0 commit comments