Search code examples
nservicebusnservicebus3

Exclude specific nservicebus host handlers (classes) in version 3.x


I have an NServiceBus host that is subscribed to some event and has its own handler (#1). This host also has a reference to an assembly which contains another handler (#2) for the same event. I want to exclude handler #2 from NServiceBus configuration, but I can't remove referenced assembly.

Important:

1) I tried to set up scanning using this: http://docs.particular.net/nservicebus/hosting/assembly-scanning

2) I use NServiceBus version 3.x


Solution

  • Remove the assembly

    NServiceBus v3 scans for assemblies. If the assembly isn't needed at runtime then just remove it so it will not be scanned.

    Just because it is referenced doesn't make it required to be deployed.

    Excluding the assembly (blacklisting)

    Exclude the assembly as mentioned in the documentation:

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

    http://docs.particular.net/nservicebus/hosting/assembly-scanning#exclude-a-list-approach

    Include assemblies or types (whitelisting)

    Only allow a specific set of assemblies or types to be scanned. Basically parsing a whitelisted range of assemblies or types.

    Assemblies:

    IEnumerable<Assembly> allowedAssembliesToScanForTypes;
    Configure.With(allowedAssembliesToScanForTypes);
    // or
    Configure.With(assembly1, assembly2);
    

    Types:

    IEnumerable<Type> allowedTypesToScan;
    Configure.With(allowedTypesToScan);
    

    Exclude specific types

    // Results in the same assembly scanning as used by NServiceBus internally
    var allTypes = from a in AllAssemblies.Except("Dummy") 
                   from t in a.GetTypes()
                   select t;
    
    // Exclude handlers that you do not want to be registered
    var allowedTypesToScan = allTypes
        .Where(t => t != typeof(EventMessageHandler)) 
        .ToList();
    
    Configure.With(allowedTypesToScan);
    

    http://docs.particular.net/nservicebus/hosting/assembly-scanning#include-a-list-approach-including-assemblies