Getting Started
Architecture
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Before Configuration Finalized

Component: NServiceBus
NuGet Package: NServiceBus (8.1)

During endpoint creation the configuration object used to construct the endpoint becomes frozen and locked. Classes that implement IWantToRunBeforeConfigurationIsFinalized are instantiated and called just before this happens. Use IWantToRunBeforeConfigurationIsFinalized for any last minute alterations to the configuration that may rely on other configuration settings.

Instances are:

  • Located by assembly scanning.
  • Created just before the configuration is frozen.
  • Created on the same thread that is creating the bus.
  • Created with Activator.CreateInstance(...) which means they:
    • Are not resolved by dependency injection (even if they are registered there).
    • Will not have any dependencies injected.
    • Must have a default constructor.

Once instantiated, Run(...) is called on each instance. These calls are made on the same thread that is creating the endpoint. The order in which instances are instantiated and run is non-deterministic and should not be relied upon.

Exceptions thrown by instances of IWantToRunBeforeConfigurationIsFinalized are unhandled by NServiceBus and will bubble up to the caller.

class RunBeforeConfigurationIsFinalized :
    IWantToRunBeforeConfigurationIsFinalized
{
    public void Run(SettingsHolder settings)
    {
        // update config instance
        settings.Set("key", "value");
        // after this config.Settings will be frozen
    }
}

Samples