By default all assemblies in the endpoint's bin
directory are scanned in search of related interfaces so that the endpoint can configure them automatically.
NServiceBus.Core.dll
) is automatically included since it is required for endpoints to properly function.Controlling the assemblies to scan
There are some cases where finer control over which assemblies are loaded is required:
- To limit the number of assemblies being scanned and hence provide improvements to startup time.
- If hosting multiple endpoints out of the same directory each endpoint may require a subset of assemblies to be loaded.
NServiceBus.RavenDB.dll
are not considered a core assembly but will still need to be included when customizing the assembly scanning.Nested Directories
Nested directories are not scanned for assemblies. Nested directory assembly scanning can be enabled using:
var scanner = endpointConfiguration.AssemblyScanner();
scanner.ScanAssembliesInNestedDirectories = true;
Assemblies to scan
An "Exclude a list" approach is used. This supports that the common scenario removing specific assemblies from scanning without the common side effect of accidentally excluding required assemblies.
Exclude a list approach
Exclude specific assemblies by name:
var scanner = endpointConfiguration.AssemblyScanner();
scanner.ExcludeAssemblies("MyAssembly1.dll", "MyAssembly2.dll");
Exclude assemblies by wildcard:
Multiple assemblies can be excluded by wildcards using the following approach:
var scanner = endpointConfiguration.AssemblyScanner();
var excludeRegexs = new List<string>
{
@"App_Web_.*\.dll",
@".*\.resources\.dll"
};
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
foreach (var fileName in Directory.EnumerateFiles(baseDirectory, "*.dll")
.Select(Path.GetFileName))
{
foreach (var pattern in excludeRegexs)
{
if (Regex.IsMatch(fileName, pattern, RegexOptions.IgnoreCase))
{
scanner.ExcludeAssemblies(fileName);
break;
}
}
}
Exclude specific types:
var scanner = endpointConfiguration.AssemblyScanner();
scanner.ExcludeTypes(type1, type2);
AppDomain assemblies
By default, the assemblies that are aleady loaded into the AppDomain are scanned. The endpoint can also be configured to disable AppDomain assembly scanning:
var scanner = endpointConfiguration.AssemblyScanner();
scanner.ScanAppDomainAssemblies = false;
Suppress scanning exceptions
By default, exceptions occurred during assembly scanning will be re-thrown. Those exceptions can be ignored using the following:
var scanner = endpointConfiguration.AssemblyScanner();
scanner.ThrowExceptions = false;
Additional assembly scanning path
Assembly scanning can be configured to scan an additional path for assemblies located outside of the default scanning path.
var scanner = endpointConfiguration.AssemblyScanner();
scanner.AdditionalAssemblyScanningPath = additionalPathToScanAssemblies;