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
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>3.1.0</Version>
<Version>4.0.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<LangVersion>preview</LangVersion>
<PackageTags>NServiceBus, Handler Ordering</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>NServiceBus.HandlerOrdering</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NServiceBus" Version="9.2.7" />
<PackageReference Include="NServiceBus" Version="10.0.0" />
<PackageReference Include="ProjectDefaults" Version="1.0.170" PrivateAssets="all" />
</ItemGroup>
</Project>
23 changes: 16 additions & 7 deletions src/HandlerOrdering/OrderHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
class OrderHandlers :
INeedInitialization
{
static readonly Lazy<MethodInfo> addHandlerMethod = new(() =>
{
var extensionsType = typeof(EndpointConfiguration).Assembly.GetType("NServiceBus.MessageHandlerRegistrationExtensions");
return extensionsType?.GetMethod("AddHandler", BindingFlags.Static | BindingFlags.Public)
?? throw new("Could not find 'AddHandler' method on MessageHandlerRegistrationExtensions. Raise an issue here https://github.com/NServiceBusCommunity/NServiceBus.Community.HandlerOrdering/issues/new");
});

public void Customize(EndpointConfiguration configuration)
{
if (configuration.GetApplyInterfaceHandlerOrdering())
Expand All @@ -13,19 +20,21 @@ static void ApplyInterfaceHandlerOrdering(EndpointConfiguration configuration)
{
var handlerDependencies = GetHandlerDependencies(configuration);
var sorted = new TypeSorter(handlerDependencies).Sorted;
configuration.ExecuteTheseHandlersFirst(sorted);

foreach (var handlerType in sorted)
{
var genericMethod = addHandlerMethod.Value.MakeGenericMethod(handlerType);
genericMethod.Invoke(null, [configuration]);
}
}

static Dictionary<Type, List<Type>> GetHandlerDependencies(EndpointConfiguration configuration)
{
var field = typeof(EndpointConfiguration)
.GetField("scannedTypes", BindingFlags.Instance | BindingFlags.NonPublic);
if (field == null)
var settings = configuration.GetSettings();
if (!settings.TryGet("TypesToScan", out List<Type> types))
{
throw new($"Could not extract 'scannedTypes' field from {nameof(EndpointConfiguration)}. Raise an issue here https://github.com/NServiceBusExtensions/NServiceBus.HandlerOrdering/issues/new");
throw new("Could not extract 'TypesToScan' from settings. Raise an issue here https://github.com/NServiceBusCommunity/NServiceBus.Community.HandlerOrdering/issues/new");
}

var types = (List<Type>) field.GetValue(configuration)!;
return GetHandlerDependencies(types);
}

Expand Down
4 changes: 4 additions & 0 deletions src/HandlerOrdering/TypeSorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public TypeSorter(Dictionary<Type, List<Type>> dependencies)
stack = new();
Visit(item);
}

Sorted = new(sorted);
}

Expand All @@ -36,8 +37,10 @@ void Visit(Type item)

throw new(stringBuilder.ToString());
}

return;
}

visited.Add(item);
if (dependencies.TryGetValue(item, out var values))
{
Expand All @@ -46,6 +49,7 @@ void Visit(Type item)
Visit(dependency);
}
}

sorted.Add(item);
}
}
2 changes: 1 addition & 1 deletion src/Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NServiceBus" Version="9.2.7" />
<PackageReference Include="NServiceBus" Version="10.0.0" />
<ProjectReference Include="..\HandlerOrdering\NServiceBus.Community.HandlerOrdering.csproj" />
</ItemGroup>
</Project>