-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathProgram.cs
More file actions
113 lines (96 loc) · 4.58 KB
/
Copy pathProgram.cs
File metadata and controls
113 lines (96 loc) · 4.58 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// This sample demonstrates opt-in unversioned fallback for per-task versioning.
// A worker can register one explicit legacy implementation for a known version
// and an unversioned implementation as the catch-all for versions that do not
// have an explicit [DurableTask(Version = "...")] registration.
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);
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");
builder.Services.AddDurableTaskWorker(wb =>
{
wb.AddTasks(tasks => tasks.AddAllGeneratedTasks());
wb.UseVersioning(new DurableTaskWorkerOptions.VersioningOptions
{
// This sample enables CatchAll fallback on both sides. Activity fallback is the safer place to
// start in your own code (activities are stateless and don't replay history); only enable
// orchestrator fallback when the unversioned orchestrator is replay-compatible with every
// version it may receive.
ActivityUnversionedFallback = DurableTaskWorkerOptions.UnversionedFallbackMode.CatchAll,
OrchestratorUnversionedFallback = DurableTaskWorkerOptions.UnversionedFallbackMode.CatchAll,
});
wb.UseWorkItemFilters();
wb.UseDurableTaskScheduler(connectionString);
});
builder.Services.AddDurableTaskClient(cb => cb.UseDurableTaskScheduler(connectionString));
IHost host = builder.Build();
await host.StartAsync();
await using DurableTaskClient client = host.Services.GetRequiredService<DurableTaskClient>();
Console.WriteLine("=== Unversioned fallback for versioned task dispatch ===");
Console.WriteLine();
SupportRequest request = new("Contoso", "BGP session down");
Console.WriteLine("Scheduling SupportWorkflow version 1.4.0 ...");
string legacyId = await client.ScheduleNewOrchestrationInstanceAsync(
nameof(SupportWorkflow),
request,
new StartOrchestrationOptions
{
Version = new TaskVersion("1.4.0"),
});
OrchestrationMetadata legacy = await client.WaitForInstanceCompletionAsync(legacyId, getInputsAndOutputs: true);
Console.WriteLine($" Result: {legacy.ReadOutputAs<string>()}");
Console.WriteLine();
Console.WriteLine("Scheduling SupportWorkflow version 1.0 ...");
string fallbackId = await client.ScheduleNewOrchestrationInstanceAsync(
nameof(SupportWorkflow),
request,
new StartOrchestrationOptions
{
Version = new TaskVersion("1.0"),
});
OrchestrationMetadata fallback = await client.WaitForInstanceCompletionAsync(fallbackId, getInputsAndOutputs: true);
Console.WriteLine($" Result: {fallback.ReadOutputAs<string>()}");
Console.WriteLine();
Console.WriteLine("Done! Version 1.4.0 used the explicit legacy class; version 1.0 used the unversioned fallback.");
await host.StopAsync();
/// <summary>
/// The current implementation. With OrchestratorUnversionedFallback enabled, this unversioned registration
/// handles every requested SupportWorkflow version that does not have an exact explicit registration.
/// </summary>
[DurableTask(nameof(SupportWorkflow))]
public sealed class SupportWorkflow : TaskOrchestrator<SupportRequest, string>
{
/// <inheritdoc />
public override Task<string> RunAsync(TaskOrchestrationContext context, SupportRequest input)
{
return Task.FromResult(
$"Current SupportWorkflow handled version '{context.Version}' for {input.Customer}: {input.Issue}");
}
}
/// <summary>
/// A pinned legacy implementation for version 1.4.0.
/// </summary>
[DurableTask(nameof(SupportWorkflow), Version = "1.4.0")]
public sealed class SupportWorkflowLegacyV140 : TaskOrchestrator<SupportRequest, string>
{
/// <inheritdoc />
public override Task<string> RunAsync(TaskOrchestrationContext context, SupportRequest input)
{
return Task.FromResult(
$"Legacy SupportWorkflow 1.4.0 handled version '{context.Version}' for {input.Customer}: {input.Issue}");
}
}
/// <summary>
/// Request input for the support workflow.
/// </summary>
public sealed record SupportRequest(string Customer, string Issue);