-
Notifications
You must be signed in to change notification settings - Fork 54
Generate extension methods in task namespace instead of Microsoft.DurableTask #538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d5eb0e8
Generate extension methods in task namespace instead of Microsoft.Dur…
YunchuWang a67299d
Address PR review comments
YunchuWang 036a9e4
Merge main and resolve sln conflict (keep both NamespaceGenerationSam…
YunchuWang 1ac7e52
Remove unused InputType/OutputType/InputParameter properties and old …
YunchuWang d80c723
Add test for cross-namespace custom type simplification
YunchuWang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
samples/NamespaceGenerationSample/NamespaceGenerationSample.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>net8.0;net10.0</TargetFrameworks> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <!-- Using p2p references so we can show latest changes in samples. --> | ||
| <ProjectReference Include="$(SrcRoot)Client/AzureManaged/Client.AzureManaged.csproj" /> | ||
| <ProjectReference Include="$(SrcRoot)Worker/AzureManaged/Worker.AzureManaged.csproj" /> | ||
| <ProjectReference Include="$(SrcRoot)Analyzers/Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> | ||
|
|
||
| <!-- Reference the source generator --> | ||
| <ProjectReference Include="$(SrcRoot)Generators/Generators.csproj" | ||
| OutputItemType="Analyzer" | ||
| ReferenceOutputAssembly="false" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // This sample demonstrates how the source generator places extension methods into the same | ||
| // namespace as the orchestrator/activity classes, keeping IDE suggestions clean and scoped. | ||
| // Tasks in different namespaces get their own GeneratedDurableTaskExtensions class. | ||
|
|
||
| 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; | ||
|
|
||
| // The generated AddAllGeneratedTasks() method is always in Microsoft.DurableTask namespace. | ||
| // Extension methods like ScheduleNewApprovalOrchestratorInstanceAsync() are in the | ||
| // NamespaceGenerationSample.Approvals namespace, and CallRegistrationActivityAsync() is in | ||
| // NamespaceGenerationSample.Registrations namespace. | ||
| using NamespaceGenerationSample.Approvals; | ||
|
|
||
| HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); | ||
|
|
||
| // Read the DTS connection string from configuration | ||
| string schedulerConnectionString = builder.Configuration.GetValue<string>("DURABLE_TASK_SCHEDULER_CONNECTION_STRING") | ||
| ?? throw new InvalidOperationException("DURABLE_TASK_SCHEDULER_CONNECTION_STRING is not set."); | ||
|
|
||
| builder.Services.AddDurableTaskClient(clientBuilder => clientBuilder.UseDurableTaskScheduler(schedulerConnectionString)); | ||
|
|
||
| builder.Services.AddDurableTaskWorker(workerBuilder => | ||
| { | ||
| // Use the generated AddAllGeneratedTasks() to register all orchestrators and activities | ||
| workerBuilder.AddTasks(tasks => tasks.AddAllGeneratedTasks()); | ||
| workerBuilder.UseDurableTaskScheduler(schedulerConnectionString); | ||
| }); | ||
|
|
||
| IHost host = builder.Build(); | ||
| await host.StartAsync(); | ||
|
|
||
| await using DurableTaskClient client = host.Services.GetRequiredService<DurableTaskClient>(); | ||
|
|
||
| // Use the generated typed extension method (in the Approvals namespace) | ||
| string instanceId = await client.ScheduleNewApprovalOrchestratorInstanceAsync("request-123"); | ||
| Console.WriteLine($"Started approval orchestration: {instanceId}"); | ||
|
|
||
| // Wait for completion | ||
| OrchestrationMetadata? result = await client.WaitForInstanceCompletionAsync( | ||
| instanceId, getInputsAndOutputs: true); | ||
| Console.WriteLine($"Orchestration completed with status: {result?.RuntimeStatus}"); | ||
| Console.WriteLine($"Output: {result?.ReadOutputAs<string>()}"); | ||
|
|
||
| await host.StopAsync(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Namespace Generation Sample | ||
|
|
||
| This sample demonstrates how the DurableTask source generator places extension methods into the same namespace as the orchestrator/activity classes. | ||
|
|
||
| ## What it shows | ||
|
|
||
| When using the `[DurableTask]` attribute on classes in different namespaces, the source generator will: | ||
|
|
||
| 1. Place extension methods (e.g., `ScheduleNewApprovalOrchestratorInstanceAsync()`, `CallRegistrationActivityAsync()`) into the **same namespace** as the task class | ||
| 2. Keep the `AddAllGeneratedTasks()` registration method in the `Microsoft.DurableTask` namespace | ||
| 3. Simplify type names within the same namespace (e.g., `MyClass` instead of `MyNS.MyClass`) | ||
|
|
||
| This results in cleaner IDE suggestions — you only see extension methods for tasks that are imported via `using` statements. | ||
|
|
||
| ## Project structure | ||
|
|
||
| - `Tasks.cs` - Defines an orchestrator in `NamespaceGenerationSample.Approvals` and an activity in `NamespaceGenerationSample.Registrations` | ||
| - `Program.cs` - Shows how to use the generated extension methods with explicit `using` statements | ||
|
|
||
| ## How to run | ||
|
|
||
| 1. Start the DTS emulator: | ||
| ```bash | ||
| docker run --name durabletask-emulator -d -p 8080:8080 -e ASPNETCORE_URLS=http://+:8080 mcr.microsoft.com/dts/dts-emulator:latest | ||
| ``` | ||
|
|
||
| 2. Set the connection string environment variable: | ||
| ```bash | ||
| export DURABLE_TASK_SCHEDULER_CONNECTION_STRING="Endpoint=http://localhost:8080;TaskHub=default;Authentication=None" | ||
| ``` | ||
|
|
||
| 3. Run the sample: | ||
| ```bash | ||
| dotnet run | ||
| ``` | ||
|
|
||
| ## Generated code | ||
|
|
||
| The source generator produces code like this: | ||
|
|
||
| ```csharp | ||
| // Extension methods in the task's namespace | ||
| namespace NamespaceGenerationSample.Approvals | ||
| { | ||
| public static class GeneratedDurableTaskExtensions | ||
| { | ||
| public static Task<string> ScheduleNewApprovalOrchestratorInstanceAsync( | ||
| this IOrchestrationSubmitter client, string input, StartOrchestrationOptions? options = null) { ... } | ||
|
|
||
| public static Task<string> CallApprovalOrchestratorAsync( | ||
| this TaskOrchestrationContext context, string input, TaskOptions? options = null) { ... } | ||
| } | ||
| } | ||
|
|
||
| namespace NamespaceGenerationSample.Registrations | ||
| { | ||
| public static class GeneratedDurableTaskExtensions | ||
| { | ||
| public static Task<string> CallRegistrationActivityAsync( | ||
| this TaskOrchestrationContext ctx, int input, TaskOptions? options = null) { ... } | ||
| } | ||
| } | ||
|
|
||
| // Registration method stays in Microsoft.DurableTask | ||
| namespace Microsoft.DurableTask | ||
| { | ||
| public static class GeneratedDurableTaskExtensions | ||
| { | ||
| internal static DurableTaskRegistry AddAllGeneratedTasks(this DurableTaskRegistry builder) { ... } | ||
| } | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // Tasks are organized into separate namespaces. The source generator will place | ||
| // each task's extension methods into its own namespace instead of Microsoft.DurableTask. | ||
|
|
||
| using Microsoft.DurableTask; | ||
| using NamespaceGenerationSample.Registrations; | ||
|
|
||
| // Approval-related tasks live in their own namespace. | ||
| // The generated ScheduleNewApprovalOrchestratorInstanceAsync() extension method | ||
| // will be generated in this namespace. The CallRegistrationActivityAsync() extension | ||
| // method is generated in the NamespaceGenerationSample.Registrations namespace. | ||
| namespace NamespaceGenerationSample.Approvals | ||
| { | ||
| /// <summary> | ||
| /// An orchestrator that runs an approval workflow. | ||
| /// The generated extension method ScheduleNewApprovalOrchestratorInstanceAsync() | ||
| /// will be in the NamespaceGenerationSample.Approvals namespace. | ||
| /// </summary> | ||
| [DurableTask(nameof(ApprovalOrchestrator))] | ||
| public class ApprovalOrchestrator : TaskOrchestrator<string, string> | ||
| { | ||
| public override async Task<string> RunAsync(TaskOrchestrationContext context, string requestId) | ||
| { | ||
| // Use the generated typed extension method (in the Registrations namespace) | ||
| // By importing the Registrations namespace, we get access to CallRegistrationActivityAsync(). | ||
| string registrationResult = await context.CallRegistrationActivityAsync(42); | ||
|
|
||
| return $"Approved request '{requestId}' with registration: {registrationResult}"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Registration-related tasks in a separate namespace. | ||
| // The generated CallRegistrationActivityAsync() extension method will be in this namespace. | ||
| namespace NamespaceGenerationSample.Registrations | ||
| { | ||
| /// <summary> | ||
| /// An activity that performs registration. | ||
| /// The generated extension method CallRegistrationActivityAsync() | ||
| /// will be in the NamespaceGenerationSample.Registrations namespace. | ||
| /// </summary> | ||
| [DurableTask(nameof(RegistrationActivity))] | ||
| public class RegistrationActivity : TaskActivity<int, string> | ||
| { | ||
| public override Task<string> RunAsync(TaskActivityContext context, int registrationId) | ||
| { | ||
| return Task.FromResult($"Registration-{registrationId} completed"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.