Skip to content

Commit 3bd6c9a

Browse files
committed
add entity sample for largepayload
1 parent ecf89de commit 3bd6c9a

File tree

1 file changed

+97
-4
lines changed

1 file changed

+97
-4
lines changed

samples/LargePayloadConsoleApp/Program.cs

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Microsoft.DurableTask;
54
using Microsoft.DurableTask.Client;
65
using Microsoft.DurableTask.Client.AzureManaged;
7-
using Microsoft.DurableTask.Converters;
6+
using Microsoft.DurableTask.Client.Entities;
7+
using Microsoft.DurableTask.Entities;
88
using Microsoft.DurableTask.Worker;
99
using Microsoft.DurableTask.Worker.AzureManaged;
10+
using Microsoft.Extensions.Configuration;
1011
using Microsoft.Extensions.DependencyInjection;
1112
using Microsoft.Extensions.Hosting;
12-
using Microsoft.Extensions.Configuration;
1313

1414
// Demonstrates Large Payload Externalization using Azure Blob Storage.
1515
// This sample uses Azurite/emulator by default via UseDevelopmentStorage=true.
@@ -24,6 +24,8 @@
2424
builder.Services.AddDurableTaskClient(b =>
2525
{
2626
b.UseDurableTaskScheduler(schedulerConnectionString);
27+
// Ensure entity APIs are enabled for the client
28+
b.Configure(o => o.EnableEntitySupport = true);
2729
b.UseExternalizedPayloads(opts =>
2830
{
2931
// Keep threshold small to force externalization for demo purposes
@@ -62,13 +64,47 @@
6264

6365
return value;
6466
});
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));
6599
});
66100
b.UseExternalizedPayloads(opts =>
67101
{
68102
opts.ExternalizeThresholdBytes = 1024; // mirror client
69103
opts.ConnectionString = builder.Configuration.GetValue<string>("DURABLETASK_STORAGE") ?? "UseDevelopmentStorage=true";
70104
opts.ContainerName = builder.Configuration.GetValue<string>("DURABLETASK_PAYLOAD_CONTAINER");
71105
});
106+
// Ensure entity APIs are enabled for the worker
107+
b.Configure(o => o.EnableEntitySupport = true);
72108
});
73109

74110
IHost host = builder.Build();
@@ -82,7 +118,7 @@
82118
Console.WriteLine($"Started orchestration with direct large input. Instance: {instanceId}");
83119

84120

85-
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(120));
121+
using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(120));
86122
OrchestrationMetadata result = await client.WaitForInstanceCompletionAsync(
87123
instanceId,
88124
getInputsAndOutputs: true,
@@ -100,3 +136,60 @@
100136

101137

102138

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

Comments
 (0)