|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// This sample demonstrates how to use IExceptionPropertiesProvider to enrich |
| 5 | +// TaskFailureDetails with custom exception properties for better diagnostics. |
| 6 | + |
| 7 | +using ExceptionPropertiesSample; |
| 8 | +using Microsoft.Extensions.Configuration; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Microsoft.DurableTask.Client; |
| 11 | +using Microsoft.DurableTask.Client.AzureManaged; |
| 12 | +using Microsoft.DurableTask.Worker; |
| 13 | +using Microsoft.DurableTask.Worker.AzureManaged; |
| 14 | +using Microsoft.Extensions.Hosting; |
| 15 | + |
| 16 | +HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); |
| 17 | + |
| 18 | +string? schedulerConnectionString = builder.Configuration.GetValue<string>("DURABLE_TASK_SCHEDULER_CONNECTION_STRING"); |
| 19 | +bool useScheduler = !string.IsNullOrWhiteSpace(schedulerConnectionString); |
| 20 | + |
| 21 | +// Register the durable task client |
| 22 | +if (useScheduler) |
| 23 | +{ |
| 24 | + builder.Services.AddDurableTaskClient(clientBuilder => clientBuilder.UseDurableTaskScheduler(schedulerConnectionString!)); |
| 25 | +} |
| 26 | +else |
| 27 | +{ |
| 28 | + builder.Services.AddDurableTaskClient().UseGrpc(); |
| 29 | +} |
| 30 | + |
| 31 | +// Register the durable task worker with custom exception properties provider |
| 32 | +if (useScheduler) |
| 33 | +{ |
| 34 | + builder.Services.AddDurableTaskWorker(workerBuilder => |
| 35 | + { |
| 36 | + workerBuilder.AddTasks(tasks => |
| 37 | + { |
| 38 | + tasks.AddOrchestrator<ValidationOrchestration>(); |
| 39 | + tasks.AddActivity<ValidateInputActivity>(); |
| 40 | + }); |
| 41 | + |
| 42 | + workerBuilder.UseDurableTaskScheduler(schedulerConnectionString!); |
| 43 | + }); |
| 44 | +} |
| 45 | +else |
| 46 | +{ |
| 47 | + builder.Services.AddDurableTaskWorker() |
| 48 | + .AddTasks(tasks => |
| 49 | + { |
| 50 | + tasks.AddOrchestrator<ValidationOrchestration>(); |
| 51 | + tasks.AddActivity<ValidateInputActivity>(); |
| 52 | + }) |
| 53 | + .UseGrpc(); |
| 54 | +} |
| 55 | + |
| 56 | +// Register the custom exception properties provider |
| 57 | +// This will automatically extract custom properties from exceptions and include them in TaskFailureDetails |
| 58 | +builder.Services.AddSingleton<IExceptionPropertiesProvider, CustomExceptionPropertiesProvider>(); |
| 59 | + |
| 60 | +IHost host = builder.Build(); |
| 61 | + |
| 62 | +// Start the worker |
| 63 | +await host.StartAsync(); |
| 64 | + |
| 65 | +// Get the client to start orchestrations |
| 66 | +DurableTaskClient client = host.Services.GetRequiredService<DurableTaskClient>(); |
| 67 | + |
| 68 | +Console.WriteLine("Exception Properties Sample"); |
| 69 | +Console.WriteLine("==========================="); |
| 70 | +Console.WriteLine(); |
| 71 | + |
| 72 | +Console.WriteLine(useScheduler |
| 73 | + ? "Configured to use Durable Task Scheduler (DTS)." |
| 74 | + : "Configured to use local gRPC. (Set DURABLE_TASK_SCHEDULER_CONNECTION_STRING to use DTS.)"); |
| 75 | +Console.WriteLine(); |
| 76 | + |
| 77 | +// Test case 1: Valid input (should succeed) |
| 78 | +Console.WriteLine("Test 1: Valid input"); |
| 79 | +string instanceId1 = await client.ScheduleNewOrchestrationInstanceAsync( |
| 80 | + "ValidationOrchestration", |
| 81 | + input: "Hello World"); |
| 82 | + |
| 83 | +OrchestrationMetadata result1 = await client.WaitForInstanceCompletionAsync( |
| 84 | + instanceId1, |
| 85 | + getInputsAndOutputs: true); |
| 86 | + |
| 87 | +if (result1.RuntimeStatus == OrchestrationRuntimeStatus.Completed) |
| 88 | +{ |
| 89 | + Console.WriteLine($"✓ Orchestration completed successfully"); |
| 90 | + Console.WriteLine($" Output: {result1.ReadOutputAs<string>()}"); |
| 91 | +} |
| 92 | +Console.WriteLine(); |
| 93 | + |
| 94 | +// Test case 2: Empty input (should fail with custom properties) |
| 95 | +Console.WriteLine("Test 2: Empty input (should fail)"); |
| 96 | +string instanceId2 = await client.ScheduleNewOrchestrationInstanceAsync( |
| 97 | + "ValidationOrchestration", |
| 98 | + input: string.Empty); |
| 99 | + |
| 100 | +OrchestrationMetadata result2 = await client.WaitForInstanceCompletionAsync( |
| 101 | + instanceId2, |
| 102 | + getInputsAndOutputs: true); |
| 103 | + |
| 104 | +if (result2.RuntimeStatus == OrchestrationRuntimeStatus.Failed && result2.FailureDetails != null) |
| 105 | +{ |
| 106 | + Console.WriteLine($"✗ Orchestration failed as expected"); |
| 107 | + Console.WriteLine($" Error Type: {result2.FailureDetails.ErrorType}"); |
| 108 | + Console.WriteLine($" Error Message: {result2.FailureDetails.ErrorMessage}"); |
| 109 | + |
| 110 | + // Display custom properties that were extracted by IExceptionPropertiesProvider |
| 111 | + if (result2.FailureDetails.Properties != null && result2.FailureDetails.Properties.Count > 0) |
| 112 | + { |
| 113 | + Console.WriteLine($" Custom Properties:"); |
| 114 | + foreach (var property in result2.FailureDetails.Properties) |
| 115 | + { |
| 116 | + Console.WriteLine($" - {property.Key}: {property.Value}"); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +Console.WriteLine(); |
| 121 | + |
| 122 | +// Test case 3: Short input (should fail with different custom properties) |
| 123 | +Console.WriteLine("Test 3: Short input (should fail)"); |
| 124 | +string instanceId3 = await client.ScheduleNewOrchestrationInstanceAsync( |
| 125 | + "ValidationOrchestration", |
| 126 | + input: "Hi"); |
| 127 | + |
| 128 | +OrchestrationMetadata result3 = await client.WaitForInstanceCompletionAsync( |
| 129 | + instanceId3, |
| 130 | + getInputsAndOutputs: true); |
| 131 | + |
| 132 | +if (result3.RuntimeStatus == OrchestrationRuntimeStatus.Failed && result3.FailureDetails != null) |
| 133 | +{ |
| 134 | + Console.WriteLine($"✗ Orchestration failed as expected"); |
| 135 | + Console.WriteLine($" Error Type: {result3.FailureDetails.ErrorType}"); |
| 136 | + Console.WriteLine($" Error Message: {result3.FailureDetails.ErrorMessage}"); |
| 137 | + |
| 138 | + // Display custom properties |
| 139 | + if (result3.FailureDetails.Properties != null && result3.FailureDetails.Properties.Count > 0) |
| 140 | + { |
| 141 | + Console.WriteLine($" Custom Properties:"); |
| 142 | + foreach (var property in result3.FailureDetails.Properties) |
| 143 | + { |
| 144 | + Console.WriteLine($" - {property.Key}: {property.Value}"); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | +Console.WriteLine(); |
| 149 | + |
| 150 | +Console.WriteLine("Sample completed. Press any key to exit..."); |
| 151 | +Console.ReadKey(); |
| 152 | + |
| 153 | +await host.StopAsync(); |
| 154 | + |
0 commit comments