dotnet/aspnetcore · error · IllegalArgumentException

The HubConnection url must be a valid url.

Error message

The HubConnection url must be a valid url.

What it means

Thrown by HubConnection.setBaseUrl when the new url is null or empty. setBaseUrl is used to redirect the connection to a different endpoint (e.g. after a negotiate redirect or for sharding) and validates its input the same way the constructor does. Distinct from constructor guard (172) - this fires when changing URL after construction.

Solutions

  1. Validate the new URL is non-null and non-empty before calling setBaseUrl.
  2. Resolve redirect URLs against a base/origin if they are relative; reject empty results explicitly.
  3. Log the candidate URL before setting it so an empty value is visible at the boundary.

Example fix

// before
String redirect = response.getHeader("Location"); // null
conn.setBaseUrl(redirect); // throws

// after
String redirect = response.getHeader("Location");
if (redirect == null || redirect.trim().isEmpty()) {
  throw new IllegalStateException("Redirect URL missing");
}
conn.setBaseUrl(redirect);
Defensive patterns

Strategy: validation

Validate before calling

void setUrlSafe(HubConnection conn, String url) {
  if (url == null || url.isEmpty()) {
    throw new IllegalArgumentException("new url is required");
  }
  conn.setBaseUrl(url);
}

Type guard

boolean isNonEmptyUrl(String url) {
  return url != null && !url.isEmpty();
}

Try / catch

try {
  conn.setBaseUrl(newUrl);
} catch (IllegalArgumentException e) {
  if ("The HubConnection url must be a valid url.".equals(e.getMessage())) {
    // resolve a non-empty url and retry
  } else throw e;
}

Prevention

When it happens

Trigger: Calling hubConnection.setBaseUrl(null) or hubConnection.setBaseUrl("") while the connection is disconnected (the disconnected-state check at 219 runs only after this passes).

Common situations: Redirecting to a URL loaded from a response header/config that resolved to null/empty; programmatic redirect logic that computed an empty string; copy/paste left the new URL blank.

Related errors


AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11). Data as JSON: /api/errors/15ba8ddc5499e4d0. Report an issue: GitHub.

Appendix: source

Thrown at src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java:215

     *
     * @return HubConnection state enum.
     */
    public HubConnectionState getConnectionState() {
        return this.state.getHubConnectionState();
    }

    // For testing only
    String getBaseUrl() {
        return this.baseUrl;
    }

    /**
     * Sets a new url for the HubConnection.
     * @param url The url to connect to.
     */
    public void setBaseUrl(String url) {
        if (url == null || url.isEmpty()) {
            throw new IllegalArgumentException("The HubConnection url must be a valid url.");
        }

        if (this.state.getHubConnectionState() != HubConnectionState.DISCONNECTED) {
            throw new IllegalStateException("The HubConnection must be in the disconnected state to change the url.");
        }

        this.baseUrl = url;
    }

    /**
     * Starts a connection to the server.
     *
     * @return A Completable that completes when the connection has been established.
     */
    public Completable start() {
        CompletableSubject localStart = CompletableSubject.create();

        this.state.lock.lock();

View on GitHub (pinned to 3600ca084e)