{"record":{"id":"8288f2c522fec234","repo":"dotnetcore/CAP","slug":"either-connection-string-or-namespace-are-required","errorCode":null,"errorMessage":"Either connection string or namespace are required.","messagePattern":"Either connection string or namespace are required\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/DotNetCore.CAP.AzureServiceBus/Helpers/ServiceBusHelpers.cs","lineNumber":13,"sourceCode":"using System;\nusing 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","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/dotnetcore/CAP/blob/e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb/src/DotNetCore.CAP.AzureServiceBus/Helpers/ServiceBusHelpers.cs#L1-L31","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Set exactly one: opt.ConnectionString = \"Endpoint=sb://...\" or opt.Namespace = \"myns.servicebus.windows.net\" (managed identity)","Prefer the string overload UseAzureServiceBus(connectionString) to guarantee the value is set","Add startup validation that asserts ConnectionString or Namespace is present before AddCap runs","If namespace is unavailable but the connection string contains Endpoint=sb://..., ensure the connection string is well-formed so the endpoint can be extracted"],"exampleFix":"// before\noptions.UseAzureServiceBus(opt => { }); // neither set\n// after\noptions.UseAzureServiceBus(opt => { opt.Namespace = \"myns.servicebus.windows.net\"; });\n// or\noptions.UseAzureServiceBus(opt => { opt.ConnectionString = cs; });","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(cs) && string.IsNullOrWhiteSpace(ns)) throw new InvalidOperationException(\"Provide either Azure Service Bus connection string or namespace\");","typeGuard":"bool HasAsbTarget(string? cs, string? ns) => !string.IsNullOrWhiteSpace(cs) || !string.IsNullOrWhiteSpace(ns);","tryCatchPattern":"try { var addr = ServiceBusHelpers.GetBrokerAddress(cs, ns); } catch (ArgumentException ex) { logger.LogCritical(ex, \"Neither connection string nor namespace configured for ASB\"); throw; }","preventionTips":["Set exactly one of ConnectionString or Namespace in AzureServiceBusOptions","Assert at startup that at least one ASB identifier is configured"],"tags":["dotnet","azure-service-bus","invalid-argument","connection-string","namespace"],"backgroundTag":"missing-required-config-field","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"}