diff --git a/eng/PatchConfig.props b/eng/PatchConfig.props index 0447c82a69..198f32e4d7 100644 --- a/eng/PatchConfig.props +++ b/eng/PatchConfig.props @@ -31,6 +31,7 @@ Later on, this will be checked using this condition: Microsoft.AspNetCore.Authentication.Google; Microsoft.AspNetCore.Http; Microsoft.AspNetCore.Server.IIS; + java:signalr; diff --git a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java index 485137d0a6..9b76b389f5 100644 --- a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java +++ b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java @@ -454,7 +454,7 @@ public class HubConnection { */ public void send(String method, Object... args) { if (hubConnectionState != HubConnectionState.CONNECTED) { - throw new RuntimeException("The 'send' method cannot be called if the connection is not active"); + throw new RuntimeException("The 'send' method cannot be called if the connection is not active."); } InvocationMessage invocationMessage = new InvocationMessage(null, method, args); @@ -472,6 +472,10 @@ public class HubConnection { */ @SuppressWarnings("unchecked") public Single invoke(Class returnType, String method, Object... args) { + if (hubConnectionState != HubConnectionState.CONNECTED) { + throw new RuntimeException("The 'invoke' method cannot be called if the connection is not active."); + } + String id = connectionState.getNextInvocationId(); InvocationMessage invocationMessage = new InvocationMessage(id, method, args); diff --git a/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java b/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java index 6c29be1c82..c46de57b3e 100644 --- a/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java +++ b/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java @@ -900,7 +900,16 @@ class HubConnectionTest { assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState()); Throwable exception = assertThrows(RuntimeException.class, () -> hubConnection.send("inc")); - assertEquals("The 'send' method cannot be called if the connection is not active", exception.getMessage()); + assertEquals("The 'send' method cannot be called if the connection is not active.", exception.getMessage()); + } + + @Test + public void cannotInvokeBeforeStart() { + HubConnection hubConnection = TestUtils.createHubConnection("http://example.com"); + assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState()); + + Throwable exception = assertThrows(RuntimeException.class, () -> hubConnection.invoke(String.class, "inc", "arg1")); + assertEquals("The 'invoke' method cannot be called if the connection is not active.", exception.getMessage()); } @Test @@ -1204,4 +1213,4 @@ class HubConnectionTest { () -> hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait()); assertEquals("Unexpected status code returned from negotiate: 500 Internal server error.", exception.getMessage()); } -} \ No newline at end of file +}