dotnet/aspnetcore · error · Error
The HubConnection must be in the Disconnected or Reconnectin
Error message
The HubConnection must be in the Disconnected or Reconnecting state to change the url.
What it means
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.
Source
Thrown at src/SignalR/clients/ts/signalr/src/HubConnection.ts:188
* in the disconnected state or if the negotiation step was skipped.
*/
get connectionId(): string | null {
return this.connection ? (this.connection.connectionId || null) : null;
}
/** Indicates the url of the {@link HubConnection} to the server. */
get baseUrl(): string {
return this.connection.baseUrl || "";
}
/**
* Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or
* Reconnecting states.
* @param {string} url The url to connect to.
*/
set baseUrl(url: string) {
if (this._connectionState !== HubConnectionState.Disconnected && this._connectionState !== HubConnectionState.Reconnecting) {
throw new Error("The HubConnection must be in the Disconnected or Reconnecting state to change the url.");
}
if (!url) {
throw new Error("The HubConnection url must be a valid url.");
}
this.connection.baseUrl = url;
}
/** Starts the connection.
*
* @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.
*/
public start(): Promise<void> {
this._startPromise = this._startWithStateTransitions();
return this._startPromise;
}
View on GitHub (pinned to 294cab2f9b)
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;`.
Example fix
// before hub.baseUrl = newUrl; // throws if Connected // after await hub.stop(); hub.baseUrl = newUrl; await hub.start();
Defensive patterns
Strategy: validation
Validate before calling
function safeSetUrl(hub, newUrl) {
if (hub.state !== HubConnectionState.Disconnected && hub.state !== HubConnectionState.Reconnecting) {
throw new Error(`Cannot change url while state is ${hub.state}`);
}
hub.baseUrl = newUrl;
} Type guard
function canChangeUrl(hub: HubConnection): boolean {
return hub.state === HubConnectionState.Disconnected || hub.state === HubConnectionState.Reconnecting;
} Try / catch
try { hub.baseUrl = newUrl; }
catch (e) {
if (/Disconnected or Reconnecting/.test(String(e))) {
await hub.stop();
hub.baseUrl = newUrl;
await hub.start();
} else throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cannot refresh authentication when the connection is not act
- Cannot refresh authentication before the connection is start
- The HubConnection url must be a valid url.
- The 'HubConnectionBuilder.withUrl' method must be called bef
- The HubConnection url must be a valid url.
AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06).
Data as JSON: /api/errors/07492988de5edd71.
Report an issue: GitHub.