Skip to content

Commit a82fce4

Browse files
Daniel Marbachramonsmits
Daniel Marbach
andauthored
Allow assemblies without extensions to be excluded (#6834) (#6839)
* Restored previous behavior of only stripping .exe / .dll * Allow assemblies without extensions to be excluded --------- Co-authored-by: Ramon Smits <[email protected]>
1 parent b22a6ab commit a82fce4

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/NServiceBus.Core.Tests/AssemblyScanner/When_exclusion_predicate_is_used.cs

+17
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,22 @@ public void No_files_explicitly_excluded_are_returned()
2929
Assert.That(explicitlySkippedDll, Is.Not.Null);
3030
Assert.That(explicitlySkippedDll.SkipReason, Contains.Substring("File was explicitly excluded from scanning"));
3131
}
32+
33+
[Test]
34+
public void Assemblies_that_have_no_extension_can_be_excluded()
35+
{
36+
var results = new AssemblyScanner(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestDlls"))
37+
{
38+
AssembliesToSkip = new List<string> { "some.random" },
39+
ScanAppDomainAssemblies = false
40+
}
41+
.GetScannableAssemblies();
42+
43+
var skippedFiles = results.SkippedFiles;
44+
var explicitlySkippedDll = skippedFiles.FirstOrDefault(s => s.FilePath.Contains("some.random.dll"));
45+
46+
Assert.That(explicitlySkippedDll, Is.Not.Null);
47+
Assert.That(explicitlySkippedDll.SkipReason, Contains.Substring("File was explicitly excluded from scanning"));
48+
}
3249
}
3350
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is not a DLL

src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ internal AssemblyScanner(Assembly assemblyToScan)
5454

5555
internal IReadOnlyCollection<string> AssembliesToSkip
5656
{
57-
set => assembliesToSkip = new HashSet<string>(value.Select(Path.GetFileNameWithoutExtension), StringComparer.OrdinalIgnoreCase);
57+
set => assembliesToSkip = new HashSet<string>(value.Select(RemoveExtension), StringComparer.OrdinalIgnoreCase);
5858
}
5959

60+
static string RemoveExtension(string assemblyName) =>
61+
assemblyName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || assemblyName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)
62+
? Path.GetFileNameWithoutExtension(assemblyName)
63+
: assemblyName;
64+
6065
internal IReadOnlyCollection<Type> TypesToSkip
6166
{
6267
set => typesToSkip = new HashSet<Type>(value);
@@ -342,9 +347,7 @@ static List<FileInfo> ScanDirectoryForAssemblyFiles(string directoryToScan, bool
342347
return fileInfo;
343348
}
344349

345-
bool IsExcluded(string assemblyNameOrFileNameWithoutExtension) =>
346-
assembliesToSkip.Contains(assemblyNameOrFileNameWithoutExtension) ||
347-
DefaultAssemblyExclusions.Contains(assemblyNameOrFileNameWithoutExtension);
350+
bool IsExcluded(string assemblyName) => assembliesToSkip.Contains(assemblyName) || DefaultAssemblyExclusions.Contains(assemblyName);
348351

349352
// The parameter and return types of this method are deliberately using the most concrete types
350353
// to avoid unnecessary allocations

0 commit comments

Comments
 (0)