{"record":{"id":"58f76632a3057c84","repo":"dotnetcore/CAP","slug":"unable-to-extract-namespace-from-connection-string","errorCode":null,"errorMessage":"Unable to extract namespace from connection string.","messagePattern":"Unable to extract namespace from connection string\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/DotNetCore.CAP.AzureServiceBus/Helpers/ServiceBusHelpers.cs","lineNumber":20,"sourceCode":"using DotNetCore.CAP.Transport;\n\nnamespace DotNetCore.CAP.AzureServiceBus.Helpers;\n\npublic static class ServiceBusHelpers\n{\n    public static BrokerAddress GetBrokerAddress(string? connectionString, string? @namespace)\n    {\n        var host = (@namespace, connectionString) switch\n        {\n            _ when string.IsNullOrWhiteSpace(@namespace) && string.IsNullOrWhiteSpace(connectionString)\n                => throw new ArgumentException(\"Either connection string or namespace are required.\"),\n            _ when string.IsNullOrWhiteSpace(connectionString)\n                   || (!string.IsNullOrWhiteSpace(@namespace) && !string.IsNullOrWhiteSpace(connectionString))\n                => @namespace!,\n            _ when string.IsNullOrWhiteSpace(@namespace)\n                => TryGetEndpointFromConnectionString(connectionString, out var extractedValue)\n                    ? extractedValue!\n                    : throw new InvalidOperationException(\"Unable to extract namespace from connection string.\"),\n            _ => throw new InvalidOperationException(\"Unhandled case in switch expression.\")\n        };\n\n        return new BrokerAddress(\"servicebus\", host);\n    }\n\n\n    private static bool TryGetEndpointFromConnectionString(string? connectionString, out string? @namespace)\n    {\n        @namespace = string.Empty;\n\n        if (string.IsNullOrWhiteSpace(connectionString))\n            return false;\n\n        var keyValuePairs = connectionString.Split(';');\n\n        foreach (var kvp in keyValuePairs)\n        {","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/dotnetcore/CAP/blob/e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb/src/DotNetCore.CAP.AzureServiceBus/Helpers/ServiceBusHelpers.cs#L2-L38","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"// before\noptions.UseAzureServiceBus(\"SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abc123\"); // no Endpoint\n// after\noptions.UseAzureServiceBus(\"Endpoint=sb://myns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abc123\");\n// or simply\noptions.UseAzureServiceBus(opt => opt.Namespace = \"myns.servicebus.windows.net\");","handlingStrategy":"validation","validationCode":"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\");","typeGuard":"bool IsNamespaceConnectionString(string? cs) => cs?.StartsWith(\"Endpoint=sb://\", StringComparison.OrdinalIgnoreCase) == true;","tryCatchPattern":"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; }","preventionTips":["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"],"tags":["dotnet","azure-service-bus","invalid-connection-string","invalid-state"],"backgroundTag":"invalid-argument-format","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"}