| Signed-off-by: Marc Duiker <marcduiker@users.noreply.github.com> | ||
|---|---|---|
| .. | ||
| ResiliencyAndCompensation | ||
| README.md | ||
| dapr.yaml | ||
| makefile | ||
| resiliency-compensation.http | ||
		
			
				
				README.md
			
		
		
			
			
		
	
	Resiliency & Compensation
This tutorial demonstrates how to improve resiliency when activities are executed and how to include compensation actions when activities return an error. For more on workflow resiliency read the Dapr docs.
Inspect the code
Open the ResiliencyAndCompensationWorkflow.cs file in the tutorials/workflow/csharp/resiliency-and-compensation/ResiliencyAndCompensation folder. This file contains the definition for the workflow. This workflow implements an activity retry policy on all the associated activities and compensating logic if an activity throws an exception.
graph LR
   SW((Start
   Workflow))
   A1[MinusOne]
   A2[Division]
   EX{Division
   Exception?}
   A3[PlusOne]
   EW((End
   Workflow))
   SW --> A1
   A1 --> A2
   A2 --> EX
   EX -- yes --> A3
   EX -- no --> EW
   A3 --> EW
Run the tutorial
- 
Use a terminal to navigate to the tutorials/workflow/csharp/resiliency-and-compensationfolder.
- 
Build the project using the .NET CLI. dotnet build ./ResiliencyAndCompensation/
- 
Use the Dapr CLI to run the Dapr Multi-App run file ```bashdapr run -f . <!-- END_STEP -->
- 
Use the POST request in the resiliency-compensation.httpfile to start the workflow with a workflow input value of1, or use this cURL command:curl -i --request POST \ --url http://localhost:5264/start/1When the workflow input is 1, theMinusOneactivity will subtract1resulting in a0. This value is passed to theDivisionactivity, which will throw an error because the divisor is0. TheDivisionactivity will be retried three times but all will fail the same way as the divisor has not changed. Finally the compensation actionPlusOnewill be executed, increasing the value back to1before returning the result.The app logs should output the following: == APP - resiliency == MinusOne: Received input: 1. == APP - resiliency == Division: Received divisor: 0. == APP - resiliency == Division: Received divisor: 0. == APP - resiliency == Division: Received divisor: 0. == APP - resiliency == PlusOne: Received input: 0.
- 
Use the GET request in the resiliency-compensation.httpfile to get the status of the workflow, or use this cURL command:curl --request GET --url http://localhost:3564/v1.0/workflows/dapr/<INSTANCEID>Where <INSTANCEID>is the workflow instance ID you received in theLocationheader in the previous step.Since `1` is used as the input, the expected serialized output of the workflow is: ```txt "1"The expected serialized custom status field of the workflow output is: "\"Compensated MinusOne activity with PlusOne activity.\""
- 
Stop the Dapr Multi-App run process by pressing Ctrl+C.