Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 31 additions & 1 deletion Microsoft.DurableTask.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32901.215
Expand Down Expand Up @@ -111,20 +111,35 @@ 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
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReplaySafeLoggerFactorySample", "samples\ReplaySafeLoggerFactorySample\ReplaySafeLoggerFactorySample.csproj", "{8E7BECBC-7226-4778-B8F2-8EBDFF0D3BA4}"
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 @@ -662,6 +677,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
{8E7BECBC-7226-4778-B8F2-8EBDFF0D3BA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E7BECBC-7226-4778-B8F2-8EBDFF0D3BA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E7BECBC-7226-4778-B8F2-8EBDFF0D3BA4}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand All @@ -674,6 +701,7 @@ Global
{8E7BECBC-7226-4778-B8F2-8EBDFF0D3BA4}.Release|x64.Build.0 = Release|Any CPU
{8E7BECBC-7226-4778-B8F2-8EBDFF0D3BA4}.Release|x86.ActiveCfg = Release|Any CPU
{8E7BECBC-7226-4778-B8F2-8EBDFF0D3BA4}.Release|x86.Build.0 = Release|Any CPU

EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -729,7 +757,9 @@ 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}
{8E7BECBC-7226-4778-B8F2-8EBDFF0D3BA4} = {EFF7632B-821E-4CFC-B4A0-ED4B24296B17}

EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AB41CB55-35EA-4986-A522-387AB3402E71}
Expand Down
25 changes: 25 additions & 0 deletions samples/NamespaceGenerationSample/NamespaceGenerationSample.csproj
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>
53 changes: 53 additions & 0 deletions samples/NamespaceGenerationSample/Program.cs
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();
72 changes: 72 additions & 0 deletions samples/NamespaceGenerationSample/README.md
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) { ... }
}
}
```
52 changes: 52 additions & 0 deletions samples/NamespaceGenerationSample/Tasks.cs
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");
}
}
}
Loading
Loading