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
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ public void find_handlers_from_included_assembly()
chainFor<Module2Message3>().ShouldNotBeNull();
chainFor<Module2Message4>().ShouldNotBeNull();
}

[Fact]
public void find_handlers_from_handler_module_assemblies()
{
with(opts =>
{
opts.Discovery.IncludeHandlerModules = true;
});

chainFor<Module2Message1>().ShouldNotBeNull();
chainFor<Module2Message2>().ShouldNotBeNull();
chainFor<Module2Message3>().ShouldNotBeNull();
chainFor<Module2Message4>().ShouldNotBeNull();
}
}

public interface IMovieSink
Expand Down Expand Up @@ -283,4 +297,4 @@ public class EventConsumer
public void Consume(Event1 @event)
{
}
}
}
4 changes: 3 additions & 1 deletion src/Testing/Module2/Module2.csproj
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\Wolverine\Wolverine.csproj" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions src/Testing/Module2/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using Wolverine.Attributes;

[assembly: WolverineHandlerModule]
10 changes: 10 additions & 0 deletions src/Wolverine/Attributes/WolverineHandlerModuleAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using JasperFx;

namespace Wolverine.Attributes;

/// <summary>
/// Assembly-level marker attribute that designates an assembly
/// as containing Wolverine message handlers for automatic discovery.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
public class WolverineHandlerModuleAttribute : JasperFxAssemblyAttribute;
21 changes: 21 additions & 0 deletions src/Wolverine/Configuration/HandlerDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ private static bool isNotPublicType(Type type)
return true;
}

/// <summary>
///
/// </summary>
public bool IncludeHandlerModules { get; set; } = false;

/// <summary>
/// Customize the conventional filtering on the handler type discovery. This is *additive* to the
/// built in conventional handler discovery. Disabling conventional discovery will negate anything
Expand Down Expand Up @@ -181,6 +186,22 @@ internal IEnumerable<Type> findAllMessages(HandlerGraph handlers)
.Distinct()
.SelectMany(actionsFromType).ToArray();
}

/// <summary>
/// Discovers and includes all assemblies marked with [WolverineHandlerModule] attribute.
/// </summary>
internal HandlerDiscovery DiscoverHandlerModules()
{
var handlerModuleAssemblies = AssemblyFinder
.FindAssemblies(a => a.HasAttribute<WolverineHandlerModuleAttribute>())
.Concat(AppDomain.CurrentDomain.GetAssemblies())
.Distinct()
.Where(a => a.HasAttribute<WolverineHandlerModuleAttribute>())
.ToArray();

Assemblies.AddRange(handlerModuleAssemblies);
return this;
}

private IEnumerable<(Type, MethodInfo)> actionsFromType(Type type)
{
Expand Down
7 changes: 6 additions & 1 deletion src/Wolverine/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ internal static IServiceCollection AddWolverine(this IServiceCollection services

configure?.Invoke(options);

if (options.Discovery.IncludeHandlerModules)
{
options.HandlerGraph.Discovery.DiscoverHandlerModules();
}

options.ApplyLazyConfiguration();

return services;
Expand Down Expand Up @@ -476,4 +481,4 @@ public void Configure(WolverineOptions options)
options.Durability.Mode = DurabilityMode.Solo;
}
}
}
}
Loading