Assembly scanning

Component: NServiceBus
NuGet Package NServiceBus (3.x)
Standard support for version 3.x of NServiceBus has expired. For more information see our Support Policy.

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

Assemblies in nested directories are scanned.

Assemblies to scan

An "Include a list" approach is used. This proved to be problematic. Many extensions rely on assembly scanning, for example transports and persistences in external NuGet packages. If, at endpoint configuration time, a list of assemblies was generated, and that list did not include extension assemblies, the endpoint would fail at runtime with some unexpected and hard to diagnose behaviors.

Exclude a list approach

Exclude specific assemblies by name:

var allAssemblies = AllAssemblies
    .Except("MyAssembly1.dll")
    .And("MyAssembly2.dll");
Configure.With(allAssemblies);

Exclude specific types:

var allTypes = from a in AllAssemblies.Except("Dummy")
               from t in a.GetTypes()
               select t;

var allowedTypesToScan = allTypes
    .Where(t => t != typeof(GenericHandler))
    .ToList();

Configure.With(allowedTypesToScan);

Include a list approach

Including assemblies:

Configure.With(myListOfAssemblies);
// or
Configure.With(assembly1, assembly2);

Controlling the exact types that NServiceBus uses:

Configure.With(myTypes);

Specifying the directory to scan:

Configure.With(@"c:\my-custom-dir");

Last modified