diff --git a/src/NServiceBus.Core.Tests/AssemblyScanner/When_exclusion_predicate_is_used.cs b/src/NServiceBus.Core.Tests/AssemblyScanner/When_exclusion_predicate_is_used.cs index 3e9d939e081..6f23db3741b 100644 --- a/src/NServiceBus.Core.Tests/AssemblyScanner/When_exclusion_predicate_is_used.cs +++ b/src/NServiceBus.Core.Tests/AssemblyScanner/When_exclusion_predicate_is_used.cs @@ -1,5 +1,6 @@ namespace NServiceBus.Core.Tests.AssemblyScanner { + using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -15,13 +16,13 @@ public void No_files_explicitly_excluded_are_returned() { var results = new AssemblyScanner(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestDlls")) { - AssembliesToSkip = new List + AssembliesToSkip = new HashSet(StringComparer.OrdinalIgnoreCase) { "dotNet.dll" }, ScanAppDomainAssemblies = false } - .GetScannableAssemblies(); + .GetScannableAssemblies(); var skippedFiles = results.SkippedFiles; var explicitlySkippedDll = skippedFiles.FirstOrDefault(s => s.FilePath.Contains("dotNet.dll")); diff --git a/src/NServiceBus.Core.Tests/Config/When_scanning_assemblies.cs b/src/NServiceBus.Core.Tests/Config/When_scanning_assemblies.cs index 1f215a34faf..752dba672d7 100644 --- a/src/NServiceBus.Core.Tests/Config/When_scanning_assemblies.cs +++ b/src/NServiceBus.Core.Tests/Config/When_scanning_assemblies.cs @@ -1,5 +1,6 @@ namespace NServiceBus.Core.Tests.Config { + using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -49,7 +50,7 @@ IEnumerable GetAssembliesInDirectory(string path, params string[] asse if (assembliesToSkip != null) { - assemblyScanner.AssembliesToSkip = assembliesToSkip.ToList(); + assemblyScanner.AssembliesToSkip = new HashSet(assembliesToSkip.ToList(), StringComparer.OrdinalIgnoreCase); } return assemblyScanner .GetScannableAssemblies() diff --git a/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs b/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs index 3d5a154918c..1f7ed635bf0 100644 --- a/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs +++ b/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs @@ -302,30 +302,9 @@ static List ScanDirectoryForAssemblyFiles(string directoryToScan, bool return fileInfo; } - bool IsExcluded(string assemblyNameOrFileName) - { - foreach (var explicitlyExcludedAssembly in AssembliesToSkip) - { - if (IsMatch(explicitlyExcludedAssembly, assemblyNameOrFileName)) - { - return true; - } - } - - foreach (var excludedByDefaultAssembly in DefaultAssemblyExclusions) - { - if (IsMatch(excludedByDefaultAssembly, assemblyNameOrFileName)) - { - return true; - } - } - - return false; - } - - static bool IsMatch(string expression1, string expression2) - => string.Equals(RemoveFileExtensionIfNecessary(expression1), RemoveFileExtensionIfNecessary(expression2), StringComparison.OrdinalIgnoreCase); + bool IsExcluded(string assemblyNameOrFileName) => AssembliesToSkip.Contains(assemblyNameOrFileName) || DefaultAssemblyExclusions.Contains(assemblyNameOrFileName); + // The input and output signature of this method is deliberate List AllowedTypes(Type[] types) { // assume the majority of types will be allowed to preallocate the list @@ -345,15 +324,6 @@ bool IsAllowedType(Type type) => Attribute.GetCustomAttribute(type, typeof(CompilerGeneratedAttribute), false) == null && !TypesToSkip.Contains(type); - static string RemoveFileExtensionIfNecessary(string assemblyOrFileName) - { - if (assemblyOrFileName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || assemblyOrFileName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) - { - return assemblyOrFileName[..^4]; - } - return assemblyOrFileName; - } - void AddTypesToResult(Assembly assembly, AssemblyScannerResults results) { try @@ -398,15 +368,11 @@ bool ShouldScanDependencies(Assembly assembly) return false; } - if (IsExcluded(assemblyName.Name)) - { - return false; - } - - return true; + // AssemblyName.Name is without the file extension + return !IsExcluded(assemblyName.Name); } - internal List AssembliesToSkip = new(); + internal HashSet AssembliesToSkip = new(StringComparer.OrdinalIgnoreCase); internal bool ScanNestedDirectories; internal HashSet TypesToSkip = new(); readonly Assembly assemblyToScan; @@ -420,7 +386,7 @@ bool ShouldScanDependencies(Assembly assembly) }; //TODO: delete when we make message scanning lazy #1617 - static readonly string[] DefaultAssemblyExclusions = + static readonly HashSet DefaultAssemblyExclusions = new(StringComparer.OrdinalIgnoreCase) { // NSB Build-Dependencies "nunit", diff --git a/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanningComponent.cs b/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanningComponent.cs index b1f522b5cbb..f9e78e5417f 100644 --- a/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanningComponent.cs +++ b/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanningComponent.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.IO; using System.Linq; using Settings; using Hosting.Helpers; @@ -31,7 +32,7 @@ public static AssemblyScanningComponent Initialize(Configuration configuration, var assemblyScannerSettings = configuration.AssemblyScannerConfiguration; - assemblyScanner.AssembliesToSkip = assemblyScannerSettings.ExcludedAssemblies; + assemblyScanner.AssembliesToSkip = new HashSet(assemblyScannerSettings.ExcludedAssemblies.Select(Path.GetFileNameWithoutExtension).ToArray(), StringComparer.OrdinalIgnoreCase); assemblyScanner.TypesToSkip = new HashSet(assemblyScannerSettings.ExcludedTypes); assemblyScanner.ScanNestedDirectories = assemblyScannerSettings.ScanAssembliesInNestedDirectories; assemblyScanner.ThrowExceptions = assemblyScannerSettings.ThrowExceptions;