{"record":{"id":"07492988de5edd71","repo":"dotnet/aspnetcore","slug":"the-hubconnection-must-be-in-the-disconnected-or-r","errorCode":null,"errorMessage":"The HubConnection must be in the Disconnected or Reconnecting state to change the url.","messagePattern":"The HubConnection must be in the Disconnected or Reconnecting state to change the url\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr/src/HubConnection.ts","lineNumber":188,"sourceCode":"     *  in the disconnected state or if the negotiation step was skipped.\n     */\n    get connectionId(): string | null {\n        return this.connection ? (this.connection.connectionId || null) : null;\n    }\n\n    /** Indicates the url of the {@link HubConnection} to the server. */\n    get baseUrl(): string {\n        return this.connection.baseUrl || \"\";\n    }\n\n    /**\n     * Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or\n     * Reconnecting states.\n     * @param {string} url The url to connect to.\n     */\n    set baseUrl(url: string) {\n        if (this._connectionState !== HubConnectionState.Disconnected && this._connectionState !== HubConnectionState.Reconnecting) {\n            throw new Error(\"The HubConnection must be in the Disconnected or Reconnecting state to change the url.\");\n        }\n\n        if (!url) {\n            throw new Error(\"The HubConnection url must be a valid url.\");\n        }\n\n        this.connection.baseUrl = url;\n    }\n\n    /** Starts the connection.\n     *\n     * @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.\n     */\n    public start(): Promise<void> {\n        this._startPromise = this._startWithStateTransitions();\n        return this._startPromise;\n    }\n","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/SignalR/clients/ts/signalr/src/HubConnection.ts#L170-L206","documentation":"Thrown in the baseUrl setter of HubConnection (HubConnection.ts:188) when an attempt is made to change the URL while the connection is not in the Disconnected or Reconnecting state. Allowing a URL change mid-Connected or Connecting would break in-flight invocations and the handshake, so the setter restricts the transition to safe states.","triggerScenarios":"Calling `hub.baseUrl = newUrl` (the setter) while `hub.state` is Connected, Connecting, or Disconnecting. The check at line 187 rejects all states other than Disconnected and Reconnecting.","commonSituations":"Trying to repoint a live connection to a different hub instance without stopping first; updating the URL in response to a user action without checking connection state; reconnect logic that swaps URLs based on telemetry while the old connection is still live.","solutions":["Stop the connection first: `await hub.stop(); hub.baseUrl = newUrl; await hub.start();`.","If using automatic reconnect, set the URL while state is Reconnecting (e.g. inside an onreconnecting callback).","Recreate the HubConnection with the new URL instead of mutating the live one.","Guard with state: `if (hub.state === HubConnectionState.Disconnected) hub.baseUrl = newUrl;`."],"exampleFix":"// before\nhub.baseUrl = newUrl; // throws if Connected\n\n// after\nawait hub.stop();\nhub.baseUrl = newUrl;\nawait hub.start();","handlingStrategy":"validation","validationCode":"function safeSetUrl(hub, newUrl) {\n  if (hub.state !== HubConnectionState.Disconnected && hub.state !== HubConnectionState.Reconnecting) {\n    throw new Error(`Cannot change url while state is ${hub.state}`);\n  }\n  hub.baseUrl = newUrl;\n}","typeGuard":"function canChangeUrl(hub: HubConnection): boolean {\n  return hub.state === HubConnectionState.Disconnected || hub.state === HubConnectionState.Reconnecting;\n}","tryCatchPattern":"try { hub.baseUrl = newUrl; }\ncatch (e) {\n  if (/Disconnected or Reconnecting/.test(String(e))) {\n    await hub.stop();\n    hub.baseUrl = newUrl;\n    await hub.start();\n  } else throw e;\n}","preventionTips":["Always check hub.state before mutating baseUrl.","Prefer stop() -> set URL -> start() as the explicit lifecycle.","Build a new HubConnection instead of repointing a live one when possible."],"tags":["lifecycle","url","validation","state"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}