-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathProgram.cs
More file actions
154 lines (134 loc) · 5.83 KB
/
Program.cs
File metadata and controls
154 lines (134 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// This sample demonstrates activity versioning with [DurableTaskVersion].
// Versioned orchestrators and versioned activities can share the same logical
// durable task names in one worker process. Plain activity calls inherit the
// orchestration instance version by default, while version-qualified helpers
// can explicitly override that routing when needed.
using Microsoft.DurableTask;
using Microsoft.DurableTask.Client;
using Microsoft.DurableTask.Client.AzureManaged;
using Microsoft.DurableTask.Worker;
using Microsoft.DurableTask.Worker.AzureManaged;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
// Read the DTS connection string from configuration.
string connectionString = builder.Configuration.GetValue<string>("DURABLE_TASK_SCHEDULER_CONNECTION_STRING")
?? throw new InvalidOperationException(
"Set DURABLE_TASK_SCHEDULER_CONNECTION_STRING. " +
"For the local emulator: Endpoint=http://localhost:8080;TaskHub=default;Authentication=None");
// AddAllGeneratedTasks() registers every [DurableTask]-annotated class in this
// project, including both versions of the orchestration and activity classes.
builder.Services.AddDurableTaskWorker(wb =>
{
wb.AddTasks(tasks => tasks.AddAllGeneratedTasks());
wb.UseDurableTaskScheduler(connectionString);
});
// Configure the client. Unlike worker-level versioning, the client does not
// stamp a single default version for every instance.
builder.Services.AddDurableTaskClient(cb => cb.UseDurableTaskScheduler(connectionString));
IHost host = builder.Build();
await host.StartAsync();
await using DurableTaskClient client = host.Services.GetRequiredService<DurableTaskClient>();
Console.WriteLine("=== Activity versioning ([DurableTaskVersion]) ===");
Console.WriteLine();
Console.WriteLine("Scheduling CheckoutWorkflow v1 ...");
string v1Id = await client.ScheduleNewCheckoutWorkflow_1InstanceAsync(5);
OrchestrationMetadata v1 = await client.WaitForInstanceCompletionAsync(v1Id, getInputsAndOutputs: true);
Console.WriteLine($" Result: {v1.ReadOutputAs<string>()}");
Console.WriteLine();
Console.WriteLine("Scheduling CheckoutWorkflow v2 ...");
string v2Id = await client.ScheduleNewCheckoutWorkflow_2InstanceAsync(5);
OrchestrationMetadata v2 = await client.WaitForInstanceCompletionAsync(v2Id, getInputsAndOutputs: true);
Console.WriteLine($" Result: {v2.ReadOutputAs<string>()}");
Console.WriteLine();
Console.WriteLine("Scheduling CheckoutWorkflow v2 with explicit ShippingQuote v1 override ...");
string overrideId = await client.ScheduleNewOrchestrationInstanceAsync(
"ExplicitOverrideCheckoutWorkflow",
input: 5,
new StartOrchestrationOptions
{
Version = new TaskVersion("2"),
});
OrchestrationMetadata overrideResult = await client.WaitForInstanceCompletionAsync(overrideId, getInputsAndOutputs: true);
Console.WriteLine($" Result: {overrideResult.ReadOutputAs<string>()}");
Console.WriteLine();
Console.WriteLine("Done! Both versions ran in the same worker process.");
Console.WriteLine("Default activity calls inherit the orchestration version, but versioned helpers can explicitly override it.");
await host.StopAsync();
/// <summary>
/// CheckoutWorkflow v1 - default activity calls inherit orchestration version "1".
/// </summary>
[DurableTask("CheckoutWorkflow")]
[DurableTaskVersion("1")]
public sealed class CheckoutWorkflowV1 : TaskOrchestrator<int, string>
{
/// <inheritdoc />
public override async Task<string> RunAsync(TaskOrchestrationContext context, int itemCount)
{
string quote = await context.CallActivityAsync<string>("ShippingQuote", itemCount);
return $"Workflow v1 -> {quote}";
}
}
/// <summary>
/// CheckoutWorkflow v2 - default activity calls inherit orchestration version "2".
/// </summary>
[DurableTask("CheckoutWorkflow")]
[DurableTaskVersion("2")]
public sealed class CheckoutWorkflowV2 : TaskOrchestrator<int, string>
{
/// <inheritdoc />
public override async Task<string> RunAsync(TaskOrchestrationContext context, int itemCount)
{
string quote = await context.CallActivityAsync<string>("ShippingQuote", itemCount);
return $"Workflow v2 -> {quote}";
}
}
/// <summary>
/// CheckoutWorkflow v2 - explicitly overrides the inherited activity version.
/// </summary>
[DurableTask("ExplicitOverrideCheckoutWorkflow")]
[DurableTaskVersion("2")]
public sealed class ExplicitOverrideCheckoutWorkflowV2 : TaskOrchestrator<int, string>
{
/// <inheritdoc />
public override async Task<string> RunAsync(TaskOrchestrationContext context, int itemCount)
{
string quote = await context.CallShippingQuote_1Async(itemCount);
return $"Workflow v2 explicit override -> {quote}";
}
}
/// <summary>
/// ShippingQuote v1 - uses a flat shipping charge.
/// </summary>
[DurableTask("ShippingQuote")]
[DurableTaskVersion("1")]
public sealed class ShippingQuoteV1 : TaskActivity<int, string>
{
/// <inheritdoc />
public override Task<string> RunAsync(TaskActivityContext context, int itemCount)
{
int total = (itemCount * 10) + 7;
return Task.FromResult($"activity v1 quote: ${total} (flat $7 shipping)");
}
}
/// <summary>
/// ShippingQuote v2 - applies a bulk discount and cheaper shipping.
/// </summary>
[DurableTask("ShippingQuote")]
[DurableTaskVersion("2")]
public sealed class ShippingQuoteV2 : TaskActivity<int, string>
{
/// <inheritdoc />
public override Task<string> RunAsync(TaskActivityContext context, int itemCount)
{
int total = (itemCount * 10) + 5;
if (itemCount >= 5)
{
total -= 10;
}
return Task.FromResult($"activity v2 quote: ${total} ($10 bulk discount + $5 shipping)");
}
}