Assembly scanning

Component: NServiceBus
NuGet Package NServiceBus (6.x)

By default all assemblies in the endpoint bin directory are scanned to find types implementing its interfaces so that it can configure them automatically.

During the scanning process, the core assembly for NServiceBus (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.
Extensions (for example NServiceBus.RavenDB.dll) are not considered a core assembly and will need to be included when customizing assembly scanning.

Nested Directories

Nested directories are not scanned for assemblies. Nested directories 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 wildcard using the following:

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);

AppDomain assemblies

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

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

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

Suppress scanning exceptions

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

By default, exceptions occurring during assembly scanning will be re-thrown. assembly scanning 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 behave incorrectly.

Last modified