Workflow management operations with DaprWorkflowClient
DaprWorkflowClient to manage workflowsWorkflow management operations with DaprWorkflowClient
The DaprWorkflowClient class provides methods to manage workflow instances. Below are the operations you can perform using the DaprWorkflowClient.
Schedule a new workflow instance
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.
var instanceId = $"order-workflow-{Guid.NewGuid().ToString()[..8]}";
var input = new Order("Paperclips", 1000, 9.95);
await daprWorkflowClient.ScheduleNewWorkflowAsync(
nameof(OrderProcessingWorkflow),
instanceId,
input);
Retrieve the status of a workflow instance
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.
var workflowStatus = await daprWorkflowClient.GetWorkflowStateAsync(instanceId);
Raise an event to a running workflow instance
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.
await daprWorkflowClient.RaiseEventAsync(instanceId, "Approval", true);
Suspend a running workflow instance
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.
await daprWorkflowClient.SuspendWorkflowAsync(instanceId);
Resume a suspended workflow instance
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.
await daprWorkflowClient.ResumeWorkflowAsync(instanceId);
Terminate a workflow instance
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.
await daprWorkflowClient.TerminateWorkflowAsync(instanceId);
Purge a workflow instance
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.
await daprWorkflowClient.PurgeWorkflowAsync(instanceId);