dotnetcore/CAP · error · ArgumentException

Either connection string or namespace are required.

Error message

Either connection string or namespace are required.

What it means

GetBrokerAddress builds the CAP BrokerAddress for Azure Service Bus from either a namespace host or a connection string. It throws ArgumentException when both are null/whitespace, because without one of them no host can be derived for the broker address.

Solutions

  1. Set exactly one: opt.ConnectionString = "Endpoint=sb://..." or opt.Namespace = "myns.servicebus.windows.net" (managed identity)
  2. Prefer the string overload UseAzureServiceBus(connectionString) to guarantee the value is set
  3. Add startup validation that asserts ConnectionString or Namespace is present before AddCap runs
  4. If namespace is unavailable but the connection string contains Endpoint=sb://..., ensure the connection string is well-formed so the endpoint can be extracted

Example fix

// before
options.UseAzureServiceBus(opt => { }); // neither set
// after
options.UseAzureServiceBus(opt => { opt.Namespace = "myns.servicebus.windows.net"; });
// or
options.UseAzureServiceBus(opt => { opt.ConnectionString = cs; });
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(cs) && string.IsNullOrWhiteSpace(ns)) throw new InvalidOperationException("Provide either Azure Service Bus connection string or namespace");

Type guard

bool HasAsbTarget(string? cs, string? ns) => !string.IsNullOrWhiteSpace(cs) || !string.IsNullOrWhiteSpace(ns);

Try / catch

try { var addr = ServiceBusHelpers.GetBrokerAddress(cs, ns); } catch (ArgumentException ex) { logger.LogCritical(ex, "Neither connection string nor namespace configured for ASB"); throw; }

Prevention

When it happens

Trigger: Constructing the broker address when AzureServiceBusOptions has neither ConnectionString nor Namespace set — e.g. options were registered but never populated, or both were explicitly set to empty strings.

Common situations: Using the delegate overload UseAzureServiceBus(opt => {}) with an empty lambda, so neither property is set; configuration binding that silently produced empty values; code paths that cleared ConnectionString assuming Namespace was set (or vice versa).

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


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

Appendix: source

Thrown at src/DotNetCore.CAP.AzureServiceBus/Helpers/ServiceBusHelpers.cs:13

using System;
using DotNetCore.CAP.Transport;

namespace DotNetCore.CAP.AzureServiceBus.Helpers;

public static class ServiceBusHelpers
{
    public static BrokerAddress GetBrokerAddress(string? connectionString, string? @namespace)
    {
        var host = (@namespace, connectionString) switch
        {
            _ when string.IsNullOrWhiteSpace(@namespace) && string.IsNullOrWhiteSpace(connectionString)
                => throw new ArgumentException("Either connection string or namespace are required."),
            _ when string.IsNullOrWhiteSpace(connectionString)
                   || (!string.IsNullOrWhiteSpace(@namespace) && !string.IsNullOrWhiteSpace(connectionString))
                => @namespace!,
            _ when string.IsNullOrWhiteSpace(@namespace)
                => TryGetEndpointFromConnectionString(connectionString, out var extractedValue)
                    ? extractedValue!
                    : throw new InvalidOperationException("Unable to extract namespace from connection string."),
            _ => throw new InvalidOperationException("Unhandled case in switch expression.")
        };

        return new BrokerAddress("servicebus", host);
    }


    private static bool TryGetEndpointFromConnectionString(string? connectionString, out string? @namespace)
    {
        @namespace = string.Empty;

View on GitHub (pinned to e52b8508e5)