dotnetcore/CAP · error · ArgumentNullException

Value cannot be null. (Parameter 'options')

Error message

Value cannot be null. (Parameter 'options')

What it means

The AzureServiceBusConsumerClient constructor reads options.Value (an IOptions<AzureServiceBusOptions>) and throws ArgumentNullException when the value itself is null. This means the Azure Service Bus options were not registered in DI, so the consumer client cannot be constructed.

Solutions

  1. Configure the options via x.AddAzureServiceBus(connectionStringOrNamespace) inside AddCap
  2. Or bind options explicitly: services.Configure<AzureServiceBusOptions>(o => o.ConnectionString = "...") before resolving the client
  3. When constructing manually, pass Options.Create(new AzureServiceBusOptions{...}) instead of null
  4. Verify the CAP options extension (AzureServiceBusOptionsExtension) is registered before bootstrap

Example fix

// before
var client = new AzureServiceBusConsumerClient(null!, logger, "sub", serviceProvider); // options.Value null
// after
services.Configure<AzureServiceBusOptions>(o => o.ConnectionString = connectionString);
var client = new AzureServiceBusConsumerClient(Options.Create(new AzureServiceBusOptions{ ConnectionString = connectionString }), logger, "sub", serviceProvider);
Defensive patterns

Strategy: validation

Validate before calling

services.Configure<AzureServiceBusOptions>(o => o.ConnectionString = cs ?? throw new InvalidOperationException("ASB options not configured"));

Type guard

bool OptionsConfigured(IOptions<AzureServiceBusOptions>? o) => o?.Value is not null;

Try / catch

try { var client = new AzureServiceBusConsumerClient(options, logger, sub, sp); } catch (ArgumentNullException ex) when (ex.ParamName == "options") { logger.LogError(ex, "AzureServiceBusOptions not registered"); throw; }

Prevention

When it happens

Trigger: Resolving or activating AzureServiceBusConsumerClient without AzureServiceBusOptions registered in the service container — e.g. constructing the client manually with a null IOptions<AzureServiceBusOptions>, or calling AddCap without the Azure Service Bus extension being registered.

Common situations: Manually new-ing up the consumer client in unit tests with default(IOptions<...>); missing x.AddAzureServiceBus(...) in CAP config so the options extension never registers; DI scope issues causing IOptions to be unpopulated.

Related errors


AI-assisted analysis of dotnetcore/CAP@e52b8508e5 (2026-09-14). Data as JSON: /api/errors/a7405ec4f5f1bf04. Report an issue: GitHub.

Appendix: source

Thrown at src/DotNetCore.CAP.AzureServiceBus/AzureServiceBusConsumerClient.cs:45

    private readonly SemaphoreSlim _semaphore;

    private ServiceBusAdministrationClient? _administrationClient;
    private ServiceBusClient? _serviceBusClient;
    private ServiceBusProcessorFacade? _serviceBusProcessor;

    public AzureServiceBusConsumerClient(
        ILogger logger,
        string subscriptionName,
        byte groupConcurrent,
        IOptions<AzureServiceBusOptions> options,
        IServiceProvider serviceProvider)
    {
        _logger = logger;
        _subscriptionName = subscriptionName;
        _groupConcurrent = groupConcurrent;
        _semaphore = new SemaphoreSlim(groupConcurrent);
        _serviceProvider = serviceProvider;
        _asbOptions = options.Value ?? throw new ArgumentNullException(nameof(options));
    }

    public Func<TransportMessage, object?, Task>? OnMessageCallback { get; set; }

    public Action<LogMessageEventArgs>? OnLogCallback { get; set; }

    public BrokerAddress BrokerAddress => ServiceBusHelpers.GetBrokerAddress(_asbOptions.ConnectionString, _asbOptions.Namespace);

    public async Task SubscribeAsync(IEnumerable<string> topics)
    {
        if (topics == null) throw new ArgumentNullException(nameof(topics));

        await ConnectAsync();

        if (!_asbOptions.AutoProvision) 
            return;

        topics = topics.Concat(_asbOptions!.SQLFilters?.Select(o => o.Key) ?? []);

View on GitHub (pinned to e52b8508e5)