{"record":{"id":"931fb9a56051b891","repo":"dotnet/aspnetcore","slug":"the-invoke-method-cannot-be-called-if-the-connec","errorCode":null,"errorMessage":"The 'invoke' method cannot be called if the connection is not active.","messagePattern":"The 'invoke' method cannot be called if the connection is not active\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java","lineNumber":675,"sourceCode":"                streams.add(stream);\n            }\n        }\n\n        return params.toArray();\n    }\n\n    /**\n     * Invokes a hub method on the server using the specified method name and arguments.\n     *\n     * @param method The name of the server method to invoke.\n     * @param args The arguments used to invoke the server method.\n     * @return A Completable that indicates when the invocation has completed.\n     */\n    public Completable invoke(String method, Object... args) {\n        this.state.lock();\n        try {\n            if (this.state.getHubConnectionState() != HubConnectionState.CONNECTED) {\n                throw new RuntimeException(\"The 'invoke' method cannot be called if the connection is not active.\");\n            }\n\n            ConnectionState connectionState = this.state.getConnectionStateUnsynchronized(false);\n            String id = connectionState.getNextInvocationId();\n\n            CompletableSubject subject = CompletableSubject.create();\n            InvocationRequest irq = new InvocationRequest(null, id);\n            connectionState.addInvocation(irq);\n\n            Subject<Object> pendingCall = irq.getPendingCall();\n\n            pendingCall.subscribe(result -> subject.onComplete(),\n                    error -> subject.onError(error),\n                    () -> subject.onComplete());\n\n            // Make sure the actual send is after setting up the callbacks otherwise there is a race\n            // where the map doesn't have the callbacks yet when the response is returned\n            sendInvocationMessage(method, args, id, false);","sourceCodeStart":657,"sourceCodeEnd":693,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java#L657-L693","documentation":"A RuntimeException thrown by the parameterless invoke(String method, Object... args) overload (returns Completable) when the connection is not CONNECTED. invoke() registers an InvocationRequest and sends an InvocationMessage expecting a server completion; doing so while disconnected would leave the request pending forever or fail unrecoverably.","triggerScenarios":"Calling connection.invoke(\"method\", args) (Completable-returning form) while getConnectionState() != CONNECTED — before start(), after stop(), or during reconnect.","commonSituations":"Invoking immediately after building without awaiting start(). Invoking from an onClosed handler or error path after disconnect. Reconnect logic racing with state transitions.","solutions":["Ensure start() has completed and state is CONNECTED before invoking.","Guard: if (connection.getConnectionState() == HubConnectionState.CONNECTED) { connection.invoke(...); }.","Defer invocations via a queue flushed on successful (re)connect."],"exampleFix":"// before\nconnection.invoke(\"DoWork\").blockingAwait(); // throws if not CONNECTED\n\n// after\nconnection.start().blockingAwait();\nif (connection.getConnectionState() == HubConnectionState.CONNECTED) {\n    connection.invoke(\"DoWork\").blockingAwait();\n}","handlingStrategy":"validation","validationCode":"if (connection.getConnectionState() == HubConnectionState.CONNECTED) {\n    connection.invoke(\"method\", arg).blockingAwait();\n} else {\n    // defer or surface a 'not connected' condition to the caller\n}","typeGuard":"boolean canInvoke = connection.getConnectionState() == HubConnectionState.CONNECTED;","tryCatchPattern":"try {\n    connection.invoke(\"method\", arg).blockingAwait();\n} catch (RuntimeException e) {\n    if (e.getMessage().contains(\"'invoke' method cannot be called\")) {\n        // not connected; retry on reconnect or report to caller\n    } else { throw e; }\n}","preventionTips":["Gate invoke() on CONNECTED state before calling.","Only invoke after start() completes; never from onClosed handlers.","Use a reconnect-aware wrapper that re-drains pending invocations."],"tags":["signalr","java","lifecycle","invoke","state-machine"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}