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
20 changes: 20 additions & 0 deletions src/Dapr.Workflow.Abstractions/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Runtime.CompilerServices;

// Allow Dapr.Workflow to call internal members (e.g. WorkflowAutoRegistry.Apply).
[assembly: InternalsVisibleTo("Dapr.Workflow, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b1f597635c44597fcecb493e2b1327033b29b1a98ac956a1a538664b68f87d45fbaada0438a15a6265e62864947cc067d8da3a7d93c5eb2fcbb850e396c8684dba74ea477d82a1bbb18932c0efb30b64ff1677f85ae833818707ac8b49ad8062ca01d2c89d8ab1843ae73e8ba9649cd28666b539444dcdee3639f95e2a099bb2")]

// Allow test projects to call internal members.
[assembly: InternalsVisibleTo("Dapr.Workflow.Abstractions.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b1f597635c44597fcecb493e2b1327033b29b1a98ac956a1a538664b68f87d45fbaada0438a15a6265e62864947cc067d8da3a7d93c5eb2fcbb850e396c8684dba74ea477d82a1bbb18932c0efb30b64ff1677f85ae833818707ac8b49ad8062ca01d2c89d8ab1843ae73e8ba9649cd28666b539444dcdee3639f95e2a099bb2")]
88 changes: 88 additions & 0 deletions src/Dapr.Workflow.Abstractions/WorkflowAutoRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Runtime.CompilerServices;

namespace Dapr.Workflow;

/// <summary>
/// Static registration point for source-generated workflow and activity auto-registrations.
/// The Dapr Workflow source generator emits a <c>[ModuleInitializer]</c> that calls
/// <see cref="Register"/> at process startup. <c>AddDaprWorkflow()</c> then calls
/// <see cref="Apply"/> to push all discovered types into <see cref="WorkflowRuntimeOptions"/>
/// before the workflow factory is built — meaning application code never has to call
/// <c>RegisterWorkflow</c> or <c>RegisterActivity</c> manually.
/// </summary>
/// <remarks>
/// Registrations stored here use first-write-wins semantics (via
/// <see cref="System.Collections.Concurrent.ConcurrentDictionary{TKey,TValue}.TryAdd"/>
/// inside <c>WorkflowsFactory</c>), so any explicit registrations made by
/// the application in its <c>AddDaprWorkflow(configure)</c> callback take precedence over
/// the auto-discovered ones without conflict.
/// </remarks>
public static class WorkflowAutoRegistry
{
private static readonly object SyncLock = new();
private static readonly List<Action<WorkflowRuntimeOptions>> Registrations = [];
private static readonly HashSet<WorkflowRuntimeOptions> AppliedOptions = new(ReferenceEqualityComparer.Instance);

/// <summary>
/// Registers a source-generated auto-registration callback.
/// Called from <c>[ModuleInitializer]</c> code emitted by the Dapr Workflow source generator.
/// Safe to call from multiple assemblies; duplicate delegates are silently ignored.
/// </summary>
/// <param name="registration">
/// An action that registers discovered workflows and activities with the provided
/// <see cref="WorkflowRuntimeOptions"/>.
/// </param>
public static void Register(Action<WorkflowRuntimeOptions> registration)
{
ArgumentNullException.ThrowIfNull(registration);

lock (SyncLock)
{
if (!Registrations.Contains(registration))
Registrations.Add(registration);
}
}

/// <summary>
/// Applies all source-generated auto-registrations to <paramref name="options"/>.
/// Called once per <see cref="WorkflowRuntimeOptions"/> instance (idempotent on repeated calls).
/// </summary>
internal static void Apply(WorkflowRuntimeOptions options)
{
ArgumentNullException.ThrowIfNull(options);

List<Action<WorkflowRuntimeOptions>> snapshot;

lock (SyncLock)
{
// Guard against multiple AddDaprWorkflow() calls that share the same options instance.
if (!AppliedOptions.Add(options))
return;

snapshot = [.. Registrations];
}

foreach (var registration in snapshot)
registration(options);
}

private sealed class ReferenceEqualityComparer : IEqualityComparer<WorkflowRuntimeOptions>
{
public static readonly ReferenceEqualityComparer Instance = new();
public bool Equals(WorkflowRuntimeOptions? x, WorkflowRuntimeOptions? y) => ReferenceEquals(x, y);
public int GetHashCode(WorkflowRuntimeOptions obj) => RuntimeHelpers.GetHashCode(obj);
}
}
5 changes: 4 additions & 1 deletion src/Dapr.Workflow.Versioning.Generators/KnownSymbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@

namespace Dapr.Workflow.Versioning;

internal sealed record KnownSymbols(INamedTypeSymbol? WorkflowBase, INamedTypeSymbol? WorkflowVersionAttribute);
internal sealed record KnownSymbols(
INamedTypeSymbol? WorkflowBase,
INamedTypeSymbol? WorkflowVersionAttribute,
INamedTypeSymbol? ActivityBase);
Loading
Loading