MassTransit/MassTransit · error · ArgumentNullException
busFactory
Error message
busFactory
What it means
SetBusFactory requires an IRegistrationBusFactory instance to configure how the bus is constructed from the container. Passing null cannot produce a bus factory, so the standard ArgumentNullException(nameof(busFactory)) is thrown immediately, before any DI registrations are made.
Solutions
- Pass a concrete IRegistrationBusFactory, e.g. new RegistrationBusFactory(...) or the transport-specific factory from the provider package
- Check the variable/expression passed for null before calling SetBusFactory
- Ensure the conditional logic that selects the transport always assigns a factory (throw on the unknown-transport branch instead)
Example fix
// before
IRegistrationBusFactory factory = SelectFactory(config); // may return null
configurator.SetBusFactory(factory);
// after
var factory = SelectFactory(config) ?? throw new InvalidOperationException("No transport configured");
configurator.SetBusFactory(factory); Defensive patterns
Strategy: validation
Validate before calling
if (busFactory == null)
throw new InvalidOperationException("busFactory must be provided before calling SetBusFactory"); Type guard
IRegistrationBusFactory f = maybeFactory ?? throw new InvalidOperationException("Missing bus factory"); Try / catch
try { configurator.SetBusFactory(factory); }
catch (ArgumentNullException ex) { logger.LogError(ex, "Bus factory was null"); throw; } Prevention
- Null-check factories returned from transport-selection code
- Prefer passing factory construction directly inline rather than through variables
When it happens
Trigger: Calling SetBusFactory(null) directly, or calling AddBus with a configure callback chain that ends up passing a null factory expression (e.g. a factory variable not yet initialized) into SetBusFactory.
Common situations: Hosting setups where the bus factory is obtained from configuration or a conditional branch (e.g. choosing RabbitMQ vs in-memory at runtime) and every branch fails to assign a value, so null reaches the call.
Related errors
- Value cannot be null. (Parameter 'configure')
- AddMassTransit() was already called and may only be called…
- An exception occurred during bus creation
- authSettings
- callback
AI-assisted analysis of MassTransit/MassTransit@62ab339afa (2026-09-13).
Data as JSON: /api/errors/ea932fd2a6cbb659.
Report an issue: GitHub.
Appendix: source
Thrown at src/MassTransit/DependencyInjection/Configuration/ServiceCollectionBusConfigurator.cs:82
protected ServiceCollectionBusConfigurator(IServiceCollection collection, IContainerRegistrar registrar)
: base(collection, registrar)
{
AddMassTransitComponents(collection);
}
protected Func<IBus, RequestTimeout, IClientFactory> CreateClientFactory { get; private set; } = DefaultClientFactory;
[Obsolete("Use 'Using[TransportName]' instead. Visit https://masstransit.io/obsolete for details.", true)]
public virtual void AddBus(Func<IBusRegistrationContext, IBusControl> busFactory)
{
SetBusFactory(new RegistrationBusFactory(busFactory));
}
public virtual void SetBusFactory<T>(T busFactory)
where T : class, IRegistrationBusFactory
{
if (busFactory == null)
throw new ArgumentNullException(nameof(busFactory));
ThrowIfAlreadyConfigured(nameof(SetBusFactory));
this.AddSingleton(provider => Bind<IBus>.Create(CreateBus(busFactory, provider)));
this.AddSingleton(provider => provider.GetRequiredService<Bind<IBus, IBusInstance>>().Value);
this.AddSingleton<IReceiveEndpointConnector>(provider => provider.GetRequiredService<Bind<IBus, IBusInstance>>().Value);
this.AddSingleton(provider => provider.GetRequiredService<Bind<IBus, IBusInstance>>().Value.BusControl);
this.AddSingleton(provider => provider.GetRequiredService<Bind<IBus, IBusInstance>>().Value.Bus);
Registrar.RegisterScopedClientFactory();
}
public virtual void AddRider(Action<IRiderRegistrationConfigurator> configure)
{
var configurator = new ServiceCollectionRiderConfigurator(this, new DependencyInjectionRiderContainerRegistrar<IBus>(this));
configure?.Invoke(configurator);
}View on GitHub (pinned to 62ab339afa)