{"record":{"id":"a7405ec4f5f1bf04","repo":"dotnetcore/CAP","slug":"value-cannot-be-null-parameter-options","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'options')","messagePattern":"Value cannot be null\\. \\(Parameter 'options'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/DotNetCore.CAP.AzureServiceBus/AzureServiceBusConsumerClient.cs","lineNumber":45,"sourceCode":"    private readonly SemaphoreSlim _semaphore;\n\n    private ServiceBusAdministrationClient? _administrationClient;\n    private ServiceBusClient? _serviceBusClient;\n    private ServiceBusProcessorFacade? _serviceBusProcessor;\n\n    public AzureServiceBusConsumerClient(\n        ILogger logger,\n        string subscriptionName,\n        byte groupConcurrent,\n        IOptions<AzureServiceBusOptions> options,\n        IServiceProvider serviceProvider)\n    {\n        _logger = logger;\n        _subscriptionName = subscriptionName;\n        _groupConcurrent = groupConcurrent;\n        _semaphore = new SemaphoreSlim(groupConcurrent);\n        _serviceProvider = serviceProvider;\n        _asbOptions = options.Value ?? throw new ArgumentNullException(nameof(options));\n    }\n\n    public Func<TransportMessage, object?, Task>? OnMessageCallback { get; set; }\n\n    public Action<LogMessageEventArgs>? OnLogCallback { get; set; }\n\n    public BrokerAddress BrokerAddress => ServiceBusHelpers.GetBrokerAddress(_asbOptions.ConnectionString, _asbOptions.Namespace);\n\n    public async Task SubscribeAsync(IEnumerable<string> topics)\n    {\n        if (topics == null) throw new ArgumentNullException(nameof(topics));\n\n        await ConnectAsync();\n\n        if (!_asbOptions.AutoProvision) \n            return;\n\n        topics = topics.Concat(_asbOptions!.SQLFilters?.Select(o => o.Key) ?? []);","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnetcore/CAP/blob/e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb/src/DotNetCore.CAP.AzureServiceBus/AzureServiceBusConsumerClient.cs#L27-L63","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Configure the options via x.AddAzureServiceBus(connectionStringOrNamespace) inside AddCap","Or bind options explicitly: services.Configure<AzureServiceBusOptions>(o => o.ConnectionString = \"...\") before resolving the client","When constructing manually, pass Options.Create(new AzureServiceBusOptions{...}) instead of null","Verify the CAP options extension (AzureServiceBusOptionsExtension) is registered before bootstrap"],"exampleFix":"// before\nvar client = new AzureServiceBusConsumerClient(null!, logger, \"sub\", serviceProvider); // options.Value null\n// after\nservices.Configure<AzureServiceBusOptions>(o => o.ConnectionString = connectionString);\nvar client = new AzureServiceBusConsumerClient(Options.Create(new AzureServiceBusOptions{ ConnectionString = connectionString }), logger, \"sub\", serviceProvider);","handlingStrategy":"validation","validationCode":"services.Configure<AzureServiceBusOptions>(o => o.ConnectionString = cs ?? throw new InvalidOperationException(\"ASB options not configured\"));","typeGuard":"bool OptionsConfigured(IOptions<AzureServiceBusOptions>? o) => o?.Value is not null;","tryCatchPattern":"try { var client = new AzureServiceBusConsumerClient(options, logger, sub, sp); } catch (ArgumentNullException ex) when (ex.ParamName == \"options\") { logger.LogError(ex, \"AzureServiceBusOptions not registered\"); throw; }","preventionTips":["Always call x.AddAzureServiceBus(...) inside AddCap so options are registered","Use Options.Create<T> when constructing the client manually in tests"],"tags":["dotnet","azure-service-bus","null-argument","dependency-injection"],"backgroundTag":"null-argument","analyzedSha":"e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb","analyzedAt":"2026-09-14T14:46:34.677Z","contentChangedAt":"2026-09-14T14:46:34.677Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}