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

Classes that implement NServiceBus.INeedInitialization are created and called as one of the first steps in the bus creation lifecycle. Use INeedInitialization to register components that will be used later in the bus creation lifecycle.

INeedInitialization exists in both the NServiceBus.Config and the NServiceBus namespaces. NServiceBus.Config.INeedInitialization is marked as obsolete and will cause a compile-time error.

Instances are:

  • Located by assembly scanning.
  • Created as one of the very first steps when the bus is created.
  • Created on the same thread that is creating the bus.
  • Created with Activator.CreateInstance(...) which means they:
    • Are not resolved from dependency injection (even if they are registered there).
    • Will not have any dependencies injected.
    • Must have a default constructor.

All calls are made in sequence on the thread that is creating the bus. The order of these calls is determined by the order of the scanned types list as a result of the assembly scan.

Exceptions thrown by instances of INeedInitialization are not handled by NServiceBus. These will bubble up to the caller creating the endpoint.

Instances of INeedInitialization are created after type-scanning has occurred. Do not attempt to alter the types to be scanned from an instance of INeedInitialization.
class NeedsInitialization :
    public void Customize(BusConfiguration busConfiguration)
        // Perform initialization
        // This is after type scanning.
        // Do NOT call the following here:
        // busConfiguration.AssembliesToScan();
        // busConfiguration.ScanAssembliesInDirectory();
        // busConfiguration.TypesToScan();


Last modified