Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Microsoft.DurableTask.sln
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,33 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExportHistory.Tests", "test
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DistributedTracingSample", "samples\DistributedTracingSample\DistributedTracingSample.csproj", "{4A7305AE-AAAE-43AE-AAB2-DA58DACC6FA8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NamespaceGenerationSample", "samples\NamespaceGenerationSample\NamespaceGenerationSample.csproj", "{5A69FD28-D814-490E-A76B-B0A5F88C25B2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Debug|x64.ActiveCfg = Debug|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Debug|x64.Build.0 = Debug|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Debug|x86.ActiveCfg = Debug|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Debug|x86.Build.0 = Debug|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Release|Any CPU.Build.0 = Release|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Release|x64.ActiveCfg = Release|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Release|x64.Build.0 = Release|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Release|x86.ActiveCfg = Release|Any CPU
{B12489CB-B7E5-497B-8F0C-F87F678947C3}.Release|x86.Build.0 = Release|Any CPU
{B0EB48BE-E4F7-4F50-B8BD-5C6172A7A584}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0EB48BE-E4F7-4F50-B8BD-5C6172A7A584}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0EB48BE-E4F7-4F50-B8BD-5C6172A7A584}.Debug|x64.ActiveCfg = Debug|Any CPU
{B0EB48BE-E4F7-4F50-B8BD-5C6172A7A584}.Debug|x64.Build.0 = Debug|Any CPU
{B0EB48BE-E4F7-4F50-B8BD-5C6172A7A584}.Debug|x86.ActiveCfg = Debug|Any CPU
{B0EB48BE-E4F7-4F50-B8BD-5C6172A7A584}.Debug|x86.Build.0 = Debug|Any CPU
Expand Down Expand Up @@ -660,6 +675,18 @@ Global
{4A7305AE-AAAE-43AE-AAB2-DA58DACC6FA8}.Release|x64.Build.0 = Release|Any CPU
{4A7305AE-AAAE-43AE-AAB2-DA58DACC6FA8}.Release|x86.ActiveCfg = Release|Any CPU
{4A7305AE-AAAE-43AE-AAB2-DA58DACC6FA8}.Release|x86.Build.0 = Release|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Debug|x64.ActiveCfg = Debug|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Debug|x64.Build.0 = Debug|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Debug|x86.ActiveCfg = Debug|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Debug|x86.Build.0 = Debug|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Release|Any CPU.Build.0 = Release|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Release|x64.ActiveCfg = Release|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Release|x64.Build.0 = Release|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Release|x86.ActiveCfg = Release|Any CPU
{5A69FD28-D814-490E-A76B-B0A5F88C25B2}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -715,6 +742,7 @@ Global
{354CE69B-78DB-9B29-C67E-0DBB862C7A65} = {8AFC9781-F6F1-4696-BB4A-9ED7CA9D612B}
{05C9EBA6-7221-D458-47D6-DA457C2F893B} = {E5637F81-2FB9-4CD7-900D-455363B142A7}
{4A7305AE-AAAE-43AE-AAB2-DA58DACC6FA8} = {EFF7632B-821E-4CFC-B4A0-ED4B24296B17}
{5A69FD28-D814-490E-A76B-B0A5F88C25B2} = {EFF7632B-821E-4CFC-B4A0-ED4B24296B17}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AB41CB55-35EA-4986-A522-387AB3402E71}
Expand Down
26 changes: 26 additions & 0 deletions samples/NamespaceGenerationSample/NamespaceGenerationSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<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/Grpc/Client.Grpc.csproj" />
<ProjectReference Include="$(SrcRoot)Worker/Grpc/Worker.Grpc.csproj" />
<ProjectReference Include="$(SrcRoot)InProcessTestHost/InProcessTestHost.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>
33 changes: 33 additions & 0 deletions samples/NamespaceGenerationSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.Testing;

// 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;

// Start the in-process test host (no external services needed)
// The generated AddAllGeneratedTasks() registers all orchestrators and activities
await using DurableTaskTestHost testHost = await DurableTaskTestHost.StartAsync(
registry => registry.AddAllGeneratedTasks());

DurableTaskClient client = testHost.Client;

// 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>()}");
67 changes: 67 additions & 0 deletions samples/NamespaceGenerationSample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 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 mcr.microsoft.com/durabletask/emulator:latest
```

2. Run the sample:
```bash
dotnet run
```
Comment thread
YunchuWang marked this conversation as resolved.

## 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) { ... }
}
}
```
51 changes: 51 additions & 0 deletions samples/NamespaceGenerationSample/Tasks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 CallRegistrationActivityAsync() and ScheduleNewApprovalOrchestratorInstanceAsync()
// extension methods will be generated in this namespace.
Comment thread
YunchuWang marked this conversation as resolved.
Outdated
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");
}
}
}
Loading
Loading