Skip to content

Commit 5aa3bed

Browse files
committed
.
1 parent e542483 commit 5aa3bed

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/HandlerOrdering/OrderHandlers.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
class OrderHandlers :
22
INeedInitialization
33
{
4+
static readonly Lazy<MethodInfo> addHandlerMethod = new(() =>
5+
{
6+
var extensionsType = typeof(EndpointConfiguration).Assembly.GetType("NServiceBus.MessageHandlerRegistrationExtensions");
7+
return extensionsType?.GetMethod("AddHandler", BindingFlags.Static | BindingFlags.Public)
8+
?? throw new("Could not find 'AddHandler' method on MessageHandlerRegistrationExtensions. Raise an issue here https://github.com/NServiceBusCommunity/NServiceBus.Community.HandlerOrdering/issues/new");
9+
});
10+
411
public void Customize(EndpointConfiguration configuration)
512
{
613
if (configuration.GetApplyInterfaceHandlerOrdering())
@@ -14,29 +21,20 @@ static void ApplyInterfaceHandlerOrdering(EndpointConfiguration configuration)
1421
var handlerDependencies = GetHandlerDependencies(configuration);
1522
var sorted = new TypeSorter(handlerDependencies).Sorted;
1623

17-
var addHandlerMethod = typeof(EndpointConfiguration).GetMethod("AddHandler", BindingFlags.Instance | BindingFlags.Public);
18-
if (addHandlerMethod == null)
19-
{
20-
throw new($"Could not find 'AddHandler' method on {nameof(EndpointConfiguration)}. Raise an issue here https://github.com/NServiceBusExtensions/NServiceBus.HandlerOrdering/issues/new");
21-
}
22-
2324
foreach (var handlerType in sorted)
2425
{
25-
var genericMethod = addHandlerMethod.MakeGenericMethod(handlerType);
26-
genericMethod.Invoke(configuration, null);
26+
var genericMethod = addHandlerMethod.Value.MakeGenericMethod(handlerType);
27+
genericMethod.Invoke(null, [configuration]);
2728
}
2829
}
2930

3031
static Dictionary<Type, List<Type>> GetHandlerDependencies(EndpointConfiguration configuration)
3132
{
32-
var field = typeof(EndpointConfiguration)
33-
.GetField("scannedTypes", BindingFlags.Instance | BindingFlags.NonPublic);
34-
if (field == null)
33+
var settings = configuration.GetSettings();
34+
if (!settings.TryGet("TypesToScan", out List<Type> types))
3535
{
36-
throw new($"Could not extract 'scannedTypes' field from {nameof(EndpointConfiguration)}. Raise an issue here https://github.com/NServiceBusExtensions/NServiceBus.HandlerOrdering/issues/new");
36+
throw new("Could not extract 'TypesToScan' from settings. Raise an issue here https://github.com/NServiceBusCommunity/NServiceBus.Community.HandlerOrdering/issues/new");
3737
}
38-
39-
var types = (List<Type>) field.GetValue(configuration)!;
4038
return GetHandlerDependencies(types);
4139
}
4240

src/HandlerOrdering/TypeSorter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public TypeSorter(Dictionary<Type, List<Type>> dependencies)
1414
stack = new();
1515
Visit(item);
1616
}
17+
1718
Sorted = new(sorted);
1819
}
1920

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

3738
throw new(stringBuilder.ToString());
3839
}
40+
3941
return;
4042
}
43+
4144
visited.Add(item);
4245
if (dependencies.TryGetValue(item, out var values))
4346
{
@@ -46,6 +49,7 @@ void Visit(Type item)
4649
Visit(dependency);
4750
}
4851
}
52+
4953
sorted.Add(item);
5054
}
5155
}

0 commit comments

Comments
 (0)