Skip to content

Commit 46a4f88

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

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
@@ -57,9 +57,14 @@ internal AssemblyScanner(Assembly assemblyToScan)
5757

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

63+
static string RemoveExtension(string assemblyName) =>
64+
assemblyName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || assemblyName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)
65+
? Path.GetFileNameWithoutExtension(assemblyName)
66+
: assemblyName;
67+
6368
internal IReadOnlyCollection<Type> TypesToSkip
6469
{
6570
set => typesToSkip = new HashSet<Type>(value);
@@ -349,9 +354,7 @@ static List<FileInfo> ScanDirectoryForAssemblyFiles(string directoryToScan, bool
349354
return fileInfo;
350355
}
351356

352-
bool IsExcluded(string assemblyNameOrFileNameWithoutExtension) =>
353-
assembliesToSkip.Contains(assemblyNameOrFileNameWithoutExtension) ||
354-
DefaultAssemblyExclusions.Contains(assemblyNameOrFileNameWithoutExtension);
357+
bool IsExcluded(string assemblyName) => assembliesToSkip.Contains(assemblyName) || DefaultAssemblyExclusions.Contains(assemblyName);
355358

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

0 commit comments

Comments
 (0)