|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -using Microsoft.DurableTask; |
5 | 4 | using Microsoft.DurableTask.Client; |
6 | 5 | using Microsoft.DurableTask.Client.AzureManaged; |
7 | | -using Microsoft.DurableTask.Converters; |
| 6 | +using Microsoft.DurableTask.Client.Entities; |
| 7 | +using Microsoft.DurableTask.Entities; |
8 | 8 | using Microsoft.DurableTask.Worker; |
9 | 9 | using Microsoft.DurableTask.Worker.AzureManaged; |
| 10 | +using Microsoft.Extensions.Configuration; |
10 | 11 | using Microsoft.Extensions.DependencyInjection; |
11 | 12 | using Microsoft.Extensions.Hosting; |
12 | | -using Microsoft.Extensions.Configuration; |
13 | 13 |
|
14 | 14 | // Demonstrates Large Payload Externalization using Azure Blob Storage. |
15 | 15 | // This sample uses Azurite/emulator by default via UseDevelopmentStorage=true. |
|
24 | 24 | builder.Services.AddDurableTaskClient(b => |
25 | 25 | { |
26 | 26 | b.UseDurableTaskScheduler(schedulerConnectionString); |
| 27 | + // Ensure entity APIs are enabled for the client |
| 28 | + b.Configure(o => o.EnableEntitySupport = true); |
27 | 29 | b.UseExternalizedPayloads(opts => |
28 | 30 | { |
29 | 31 | // Keep threshold small to force externalization for demo purposes |
|
62 | 64 |
|
63 | 65 | return value; |
64 | 66 | }); |
| 67 | + |
| 68 | + // Entity samples |
| 69 | + // 1) Large entity operation input (worker externalizes input; entity receives resolved payload) |
| 70 | + tasks.AddOrchestratorFunc<object?, int>( |
| 71 | + "LargeEntityOperationInput", |
| 72 | + (ctx, _) => ctx.Entities.CallEntityAsync<int>( |
| 73 | + new EntityInstanceId(nameof(EchoLengthEntity), "1"), |
| 74 | + operationName: "EchoLength", |
| 75 | + input: new string('E', 700 * 1024))); |
| 76 | + tasks.AddEntity<EchoLengthEntity>(nameof(EchoLengthEntity)); |
| 77 | + |
| 78 | + // 2) Large entity operation output (worker externalizes output; orchestrator reads resolved payload) |
| 79 | + tasks.AddOrchestratorFunc<object?, int>( |
| 80 | + "LargeEntityOperationOutput", |
| 81 | + async (ctx, _) => (await ctx.Entities.CallEntityAsync<string>( |
| 82 | + new EntityInstanceId(nameof(LargeResultEntity), "1"), |
| 83 | + operationName: "Produce", |
| 84 | + input: 850 * 1024)).Length); |
| 85 | + tasks.AddEntity<LargeResultEntity>(nameof(LargeResultEntity)); |
| 86 | + |
| 87 | + // 3) Large entity state (worker externalizes state; client resolves on query) |
| 88 | + tasks.AddOrchestratorFunc<object?, object?>( |
| 89 | + "LargeEntityState", |
| 90 | + async (ctx, _) => |
| 91 | + { |
| 92 | + await ctx.Entities.CallEntityAsync( |
| 93 | + new EntityInstanceId(nameof(StateEntity), "1"), |
| 94 | + operationName: "Set", |
| 95 | + input: new string('S', 900 * 1024)); |
| 96 | + return null; |
| 97 | + }); |
| 98 | + tasks.AddEntity<StateEntity>(nameof(StateEntity)); |
65 | 99 | }); |
66 | 100 | b.UseExternalizedPayloads(opts => |
67 | 101 | { |
68 | 102 | opts.ExternalizeThresholdBytes = 1024; // mirror client |
69 | 103 | opts.ConnectionString = builder.Configuration.GetValue<string>("DURABLETASK_STORAGE") ?? "UseDevelopmentStorage=true"; |
70 | 104 | opts.ContainerName = builder.Configuration.GetValue<string>("DURABLETASK_PAYLOAD_CONTAINER"); |
71 | 105 | }); |
| 106 | + // Ensure entity APIs are enabled for the worker |
| 107 | + b.Configure(o => o.EnableEntitySupport = true); |
72 | 108 | }); |
73 | 109 |
|
74 | 110 | IHost host = builder.Build(); |
|
82 | 118 | Console.WriteLine($"Started orchestration with direct large input. Instance: {instanceId}"); |
83 | 119 |
|
84 | 120 |
|
85 | | -using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(120)); |
| 121 | +using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(120)); |
86 | 122 | OrchestrationMetadata result = await client.WaitForInstanceCompletionAsync( |
87 | 123 | instanceId, |
88 | 124 | getInputsAndOutputs: true, |
|
100 | 136 |
|
101 | 137 |
|
102 | 138 |
|
| 139 | +// Run entity samples |
| 140 | +Console.WriteLine(); |
| 141 | +Console.WriteLine("Running LargeEntityOperationInput..."); |
| 142 | +string entityInputInstance = await client.ScheduleNewOrchestrationInstanceAsync("LargeEntityOperationInput"); |
| 143 | +OrchestrationMetadata entityInputResult = await client.WaitForInstanceCompletionAsync(entityInputInstance, getInputsAndOutputs: true, cts.Token); |
| 144 | +Console.WriteLine($"Status: {entityInputResult.RuntimeStatus}, Output length: {entityInputResult.ReadOutputAs<int>()}"); |
| 145 | + |
| 146 | +Console.WriteLine(); |
| 147 | +Console.WriteLine("Running LargeEntityOperationOutput..."); |
| 148 | +string entityOutputInstance = await client.ScheduleNewOrchestrationInstanceAsync("LargeEntityOperationOutput"); |
| 149 | +OrchestrationMetadata entityOutputResult = await client.WaitForInstanceCompletionAsync(entityOutputInstance, getInputsAndOutputs: true, cts.Token); |
| 150 | +Console.WriteLine($"Status: {entityOutputResult.RuntimeStatus}, Output length: {entityOutputResult.ReadOutputAs<int>()}"); |
| 151 | + |
| 152 | +Console.WriteLine(); |
| 153 | +Console.WriteLine("Running LargeEntityState and querying state..."); |
| 154 | +string entityStateInstance = await client.ScheduleNewOrchestrationInstanceAsync("LargeEntityState"); |
| 155 | +OrchestrationMetadata entityStateOrch = await client.WaitForInstanceCompletionAsync(entityStateInstance, getInputsAndOutputs: true, cts.Token); |
| 156 | +Console.WriteLine($"Status: {entityStateOrch.RuntimeStatus}"); |
| 157 | +EntityMetadata<string>? state = await client.Entities.GetEntityAsync<string>(new EntityInstanceId(nameof(StateEntity), "1"), includeState: true); |
| 158 | +Console.WriteLine($"State length: {state?.State?.Length ?? 0}"); |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +public class EchoLengthEntity : TaskEntity<int> |
| 165 | +{ |
| 166 | + public int EchoLength(string input) |
| 167 | + { |
| 168 | + return input.Length; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +public class LargeResultEntity : TaskEntity<object?> |
| 173 | +{ |
| 174 | + public string Produce(int length) |
| 175 | + { |
| 176 | + return new string('R', length); |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +public class StateEntity : TaskEntity<string?> |
| 181 | +{ |
| 182 | + protected override string? InitializeState(TaskEntityOperation entityOperation) |
| 183 | + { |
| 184 | + // Avoid Activator.CreateInstance<string>() which throws; start as null (no state) |
| 185 | + return null; |
| 186 | + } |
| 187 | + |
| 188 | + public void Set(string value) |
| 189 | + { |
| 190 | + this.State = value; |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | + |
| 195 | + |
0 commit comments