From e542483b6b83918de7700dca16a287e20b29c2ca Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 30 Jan 2026 22:35:07 +1100 Subject: [PATCH 1/2] update to NSB 10 --- src/Directory.Build.props | 2 +- .../NServiceBus.Community.HandlerOrdering.csproj | 4 ++-- src/HandlerOrdering/OrderHandlers.cs | 13 ++++++++++++- src/Sample/Sample.csproj | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 3caa372..93369c0 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,7 +1,7 @@ - 3.1.0 + 4.0.0 1.0.0 preview NServiceBus, Handler Ordering diff --git a/src/HandlerOrdering/NServiceBus.Community.HandlerOrdering.csproj b/src/HandlerOrdering/NServiceBus.Community.HandlerOrdering.csproj index e516235..ccdb363 100644 --- a/src/HandlerOrdering/NServiceBus.Community.HandlerOrdering.csproj +++ b/src/HandlerOrdering/NServiceBus.Community.HandlerOrdering.csproj @@ -1,11 +1,11 @@ - net9.0 + net10.0 NServiceBus.HandlerOrdering - + \ No newline at end of file diff --git a/src/HandlerOrdering/OrderHandlers.cs b/src/HandlerOrdering/OrderHandlers.cs index 0f1a542..e68b173 100644 --- a/src/HandlerOrdering/OrderHandlers.cs +++ b/src/HandlerOrdering/OrderHandlers.cs @@ -13,7 +13,18 @@ static void ApplyInterfaceHandlerOrdering(EndpointConfiguration configuration) { var handlerDependencies = GetHandlerDependencies(configuration); var sorted = new TypeSorter(handlerDependencies).Sorted; - configuration.ExecuteTheseHandlersFirst(sorted); + + var addHandlerMethod = typeof(EndpointConfiguration).GetMethod("AddHandler", BindingFlags.Instance | BindingFlags.Public); + if (addHandlerMethod == null) + { + throw new($"Could not find 'AddHandler' method on {nameof(EndpointConfiguration)}. Raise an issue here https://github.com/NServiceBusExtensions/NServiceBus.HandlerOrdering/issues/new"); + } + + foreach (var handlerType in sorted) + { + var genericMethod = addHandlerMethod.MakeGenericMethod(handlerType); + genericMethod.Invoke(configuration, null); + } } static Dictionary> GetHandlerDependencies(EndpointConfiguration configuration) diff --git a/src/Sample/Sample.csproj b/src/Sample/Sample.csproj index 1f9e7fd..aba13e6 100644 --- a/src/Sample/Sample.csproj +++ b/src/Sample/Sample.csproj @@ -5,7 +5,7 @@ 7.1 - + \ No newline at end of file From 5aa3bed7297cadbbb96342adf57b268500cc9609 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 30 Jan 2026 23:00:18 +1100 Subject: [PATCH 2/2] . --- src/HandlerOrdering/OrderHandlers.cs | 26 ++++++++++++-------------- src/HandlerOrdering/TypeSorter.cs | 4 ++++ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/HandlerOrdering/OrderHandlers.cs b/src/HandlerOrdering/OrderHandlers.cs index e68b173..919afc8 100644 --- a/src/HandlerOrdering/OrderHandlers.cs +++ b/src/HandlerOrdering/OrderHandlers.cs @@ -1,6 +1,13 @@ class OrderHandlers : INeedInitialization { + static readonly Lazy 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()) @@ -14,29 +21,20 @@ static void ApplyInterfaceHandlerOrdering(EndpointConfiguration configuration) var handlerDependencies = GetHandlerDependencies(configuration); var sorted = new TypeSorter(handlerDependencies).Sorted; - var addHandlerMethod = typeof(EndpointConfiguration).GetMethod("AddHandler", BindingFlags.Instance | BindingFlags.Public); - if (addHandlerMethod == null) - { - throw new($"Could not find 'AddHandler' method on {nameof(EndpointConfiguration)}. Raise an issue here https://github.com/NServiceBusExtensions/NServiceBus.HandlerOrdering/issues/new"); - } - foreach (var handlerType in sorted) { - var genericMethod = addHandlerMethod.MakeGenericMethod(handlerType); - genericMethod.Invoke(configuration, null); + var genericMethod = addHandlerMethod.Value.MakeGenericMethod(handlerType); + genericMethod.Invoke(null, [configuration]); } } static Dictionary> 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 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) field.GetValue(configuration)!; return GetHandlerDependencies(types); } diff --git a/src/HandlerOrdering/TypeSorter.cs b/src/HandlerOrdering/TypeSorter.cs index 8aa186b..87aa0d3 100644 --- a/src/HandlerOrdering/TypeSorter.cs +++ b/src/HandlerOrdering/TypeSorter.cs @@ -14,6 +14,7 @@ public TypeSorter(Dictionary> dependencies) stack = new(); Visit(item); } + Sorted = new(sorted); } @@ -36,8 +37,10 @@ void Visit(Type item) throw new(stringBuilder.ToString()); } + return; } + visited.Add(item); if (dependencies.TryGetValue(item, out var values)) { @@ -46,6 +49,7 @@ void Visit(Type item) Visit(dependency); } } + sorted.Add(item); } }