{"record":{"id":"ac8112f0f43fb565","repo":"dotnet/aspnetcore","slug":"the-hubconnectionbuilder-withurl-method-must-be","errorCode":null,"errorMessage":"The 'HubConnectionBuilder.withUrl' method must be called before building the connection.","messagePattern":"The 'HubConnectionBuilder\\.withUrl' method must be called before building the connection\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr/src/HubConnectionBuilder.ts","lineNumber":257,"sourceCode":"\n    /** Creates a {@link @microsoft/signalr.HubConnection} from the configuration options specified in this builder.\n     *\n     * @returns {HubConnection} The configured {@link @microsoft/signalr.HubConnection}.\n     */\n    public build(): HubConnection {\n        // If httpConnectionOptions has a logger, use it. Otherwise, override it with the one\n        // provided to configureLogger\n        const httpConnectionOptions = this.httpConnectionOptions || {};\n\n        // If it's 'null', the user **explicitly** asked for null, don't mess with it.\n        if (httpConnectionOptions.logger === undefined) {\n            // If our logger is undefined or null, that's OK, the HttpConnection constructor will handle it.\n            httpConnectionOptions.logger = this.logger;\n        }\n\n        // Now create the connection\n        if (!this.url) {\n            throw new Error(\"The 'HubConnectionBuilder.withUrl' method must be called before building the connection.\");\n        }\n        const connection = new HttpConnection(this.url, httpConnectionOptions);\n\n        return HubConnection.create(\n            connection,\n            this.logger || NullLogger.instance,\n            this.protocol || new JsonHubProtocol(),\n            this.reconnectPolicy,\n            this._serverTimeoutInMilliseconds,\n            this._keepAliveIntervalInMilliseconds,\n            this._statefulReconnectBufferSize,\n            this._authenticationRefreshOptions);\n    }\n}\n\nfunction isLogger(logger: any): logger is ILogger {\n    return logger.log !== undefined;\n}","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/SignalR/clients/ts/signalr/src/HubConnectionBuilder.ts#L239-L275","documentation":"Thrown at HubConnectionBuilder.ts:257 by `build()` when `this.url` is falsy, meaning `withUrl()` was never called. The builder requires an HTTP-based URL to construct the underlying HttpConnection; without it, build cannot proceed. The url field is only set by `withUrl` (line 134).","triggerScenarios":"Calling `new HubConnectionBuilder().configureLogging(...).build()` without ever invoking `withUrl(url)`. The guard at line 256 fires.","commonSituations":"Conditional setup that skipped the withUrl branch (e.g. URL came from async config but the builder was built synchronously before the await resolved); refactoring that removed the withUrl line; copy-paste that left only configureLogging/withAutomaticReconnect; typo calling a non-existent `withHubUrl`.","solutions":["Always call `.withUrl(url)` before `.build()`.","If the URL is async, await it before building rather than building with a placeholder.","Assert the builder is fully configured in tests: `if (!builder.url) throw ...` before build.","Use a factory function that takes url as a required parameter so it can't be forgotten."],"exampleFix":"// before\nconst hub = new HubConnectionBuilder().configureLogging(LogLevel.Information).build();\n\n// after\nconst hub = new HubConnectionBuilder()\n  .configureLogging(LogLevel.Information)\n  .withUrl(url)\n  .build();","handlingStrategy":"validation","validationCode":"function buildConnection(builder, url) {\n  if (!url) throw new Error('withUrl must be called with a non-empty url before build');\n  if (!(builder as any).url) builder.withUrl(url);\n  return builder.build();\n}","typeGuard":"function builderHasUrl(b: HubConnectionBuilder): boolean {\n  return !!(b as any).url;\n}","tryCatchPattern":"try { return builder.build(); }\ncatch (e) {\n  if (/withUrl' method must be called/.test(String(e))) {\n    throw new Error('Programming bug: withUrl(url) was never invoked before build()');\n  }\n  throw e;\n}","preventionTips":["Always call .withUrl(url) before .build().","If URL is async, await it before building.","Encapsulate builder setup in a factory that takes url as a required param."],"tags":["builder","configuration","url","validation"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}