Assembly scanning

Component: NServiceBus
Standard support for version 6.x of NServiceBus has expired. For more information see our Support Policy.

NServiceBus scans assemblies at endpoint startup to automatically detect and load message types, message handlers, features, and installers.

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 extensions such as NServiceBus.RavenDB.dll are not considered a core assembly but still must be included when customizing the assembly scanning.

AppDomain assemblies

This configuration option is only available in NServiceBus 6.2 and above.

By default, the assemblies that are already loaded into the AppDomain, but are not present in the application's base directory, are not scanned. The endpoint can also be configured to scan the AppDomain assemblies:

var scanner = endpointConfiguration.AssemblyScanner();
scanner.ScanAppDomainAssemblies = true;

Assembly files

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.

Nested directories

Nested directories are not scanned for assemblies by default. Nested directory assembly scanning can be enabled using:

var scanner = endpointConfiguration.AssemblyScanner();
scanner.ScanAssembliesInNestedDirectories = true;

Assemblies to scan

The assemblies being scanned can be further controlled via user-defined exclusions. This supports common scenarios removing specific assemblies from scanning without the risk of accidentally excluding required assemblies.

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>

var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
foreach (var fileName in Directory.EnumerateFiles(baseDirectory, "*.dll")
    foreach (var pattern in excludeRegexs)
        if (Regex.IsMatch(fileName, pattern, RegexOptions.IgnoreCase))

Exclude specific types

var scanner = endpointConfiguration.AssemblyScanner();
scanner.ExcludeTypes(type1, type2);

Suppress scanning exceptions

This configuration option is only available in NServiceBus 6.2 and above.

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;
Ignoring assembly scanning exceptions can cause the endpoint to not load some features, behaviors, messages or message handlers and cause incorrect behavior.

Last modified