dotnetcore/CAP · error · InvalidOperationException
Unable to extract namespace from connection string.
Error message
Unable to extract namespace from connection string.
What it means
When only a connection string is provided (no namespace), GetBrokerAddress tries to extract the host from the Endpoint=sb://... part of the connection string. If extraction fails — the connection string is malformed or lacks an Endpoint component — it throws InvalidOperationException.
Solutions
- Use a full namespace connection string that starts with Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=...;SharedAccessKey=...
- Alternatively set opt.Namespace directly so the connection string does not need parsing
- Validate the connection string format (starts with 'Endpoint=sb://') before configuring CAP
- Re-copy the connection string from Azure Portal -> Service Bus Namespace -> Shared access policies (root policy for namespace-level access)
Example fix
// before
options.UseAzureServiceBus("SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abc123"); // no Endpoint
// after
options.UseAzureServiceBus("Endpoint=sb://myns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abc123");
// or simply
options.UseAzureServiceBus(opt => opt.Namespace = "myns.servicebus.windows.net"); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(ns) && !(cs?.StartsWith("Endpoint=sb://", StringComparison.OrdinalIgnoreCase) ?? false)) throw new InvalidOperationException("Connection string must contain Endpoint=sb:// when namespace is not set"); Type guard
bool IsNamespaceConnectionString(string? cs) => cs?.StartsWith("Endpoint=sb://", StringComparison.OrdinalIgnoreCase) == true; Try / catch
try { var addr = ServiceBusHelpers.GetBrokerAddress(cs, null); } catch (InvalidOperationException ex) when (ex.Message.Contains("Unable to extract namespace")) { logger.LogCritical(ex, "Connection string lacks Endpoint=sb://"); throw; } Prevention
- Copy the full namespace-level connection string from the Azure Portal, not entity/key-only fragments
- Prefer setting Namespace explicitly to avoid connection-string parsing
- Trim environment-injected values and verify no truncation in secrets pipelines
When it happens
Trigger: Passing a connection string without Endpoint=sb://<host>/ (e.g. only SharedAccessKeyName/SharedAccessKey pairs, a truncated string, or an Event-Hub-style string with a different endpoint key) while Namespace is null/empty.
Common situations: Copying an SAS entity connection string that still includes EntityPath but was truncated; pasting keys from the portal without the Endpoint segment; whitespace/encoding corruption when injecting the string via environment variables; mistakenly using a storage or Event Hubs connection string.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- Value cannot be null. (Parameter 'options')
- Value cannot be null. (Parameter 'topics')
- Value cannot be null. (Parameter 'connectionString')
- Value cannot be null. (Parameter 'configure')
- Either connection string or namespace are required.
AI-assisted analysis of dotnetcore/CAP@e52b8508e5 (2026-09-14).
Data as JSON: /api/errors/58f76632a3057c84.
Report an issue: GitHub.
Appendix: source
Thrown at src/DotNetCore.CAP.AzureServiceBus/Helpers/ServiceBusHelpers.cs:20
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;
if (string.IsNullOrWhiteSpace(connectionString))
return false;
var keyValuePairs = connectionString.Split(';');
foreach (var kvp in keyValuePairs)
{View on GitHub (pinned to e52b8508e5)