Skip to content

Commit 0622882

Browse files
Add troubleshooting page for SignalR (#20125)
* Add troubleshooting page for SignalR * header * Apply suggestions from code review Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * some fb * Apply suggestions from code review Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * fb Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>
1 parent 7691d28 commit 0622882

3 files changed

Lines changed: 99 additions & 40 deletions

File tree

aspnetcore/signalr/javascript-client.md

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -262,46 +262,6 @@ The following code demonstrates a typical manual reconnection approach:
262262

263263
A real-world implementation would use an exponential back-off or retry a specified number of times before giving up.
264264

265-
## Troubleshoot WebSocket handshake errors
266-
267-
This section provides help on the *"Error during WebSocket handshake"* exception that occurs when trying to establish connection to ASP.NET Core SignalR hub.
268-
269-
### Response code 400 or 503
270-
271-
For the following error:
272-
273-
```log
274-
WebSocket connection to 'wss://xxx/HubName' failed: Error during WebSocket handshake: Unexpected response code: 400
275-
276-
Error: Failed to start the connection: Error: There was an error with the transport.
277-
```
278-
279-
The error is usually caused by client only use WebSockets transport but WebSockets protocol is not enabled on server.
280-
281-
### Response code 307
282-
283-
```log
284-
WebSocket connection to 'ws://xxx/HubName' failed: Error during WebSocket handshake: Unexpected response code: 307
285-
```
286-
287-
Frequently this occurs when the SignalR hub server:
288-
289-
* Listens to and responds over both HTTP and HTTPS.
290-
* Is configured to enforce HTTPS by calling `UseHttpsRedirection` in `Startup`, or enforces HTTPS via URL rewrite rule.
291-
292-
This error can be caused by specifying the HTTP URL on client side using `.withUrl("http://xxx/HubName")`. The fix for this case is modifying the code to use an HTTPS URL.
293-
294-
### Response code 404
295-
296-
```log
297-
WebSocket connection to 'wss://xxx/HubName' failed: Error during WebSocket handshake: Unexpected response code: 404
298-
```
299-
300-
If the app works on localhost, but returns this error after publishing to IIS server:
301-
302-
* Verify the ASP.NET Core SignalR app is hosted as an IIS sub-application.
303-
* Don't set URL with the sub-app's pathbase on SignalR JavaScript client side `.withUrl("/SubAppName/HubName")`.
304-
305265
## Additional resources
306266

307267
* [JavaScript API reference](/javascript/api/?view=signalr-js-latest&preserve-view=true )
@@ -312,6 +272,7 @@ If the app works on localhost, but returns this error after publishing to IIS se
312272
* [Publish to Azure](xref:signalr/publish-to-azure-web-app)
313273
* [Cross-Origin Requests (CORS)](xref:security/cors)
314274
* [Azure SignalR Service serverless documentation](/azure/azure-signalr/signalr-concept-serverless-development-config)
275+
* [Troubleshoot connection errors](xref:signalr/troubleshoot)
315276

316277
::: moniker-end
317278

aspnetcore/signalr/troubleshoot.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: ASP.NET Core SignalR connection troubleshooting
3+
author: bradygaster
4+
description: ASP.NET Core SignalR connection troubleshooting.
5+
monikerRange: '>= aspnetcore-2.1'
6+
ms.author: bradyg
7+
ms.custom: mvc
8+
ms.date: 04/08/2020
9+
no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
10+
uid: signalr/troubleshoot
11+
---
12+
# Troubleshoot connection errors
13+
14+
This section provides help with errors that can occur when trying to establish a connection to a ASP.NET Core SignalR hub.
15+
16+
### Response code 404
17+
18+
When using WebSockets and `skipNegotiation = true`
19+
```log
20+
WebSocket connection to 'wss://xxx/HubName' failed: Error during WebSocket handshake: Unexpected response code: 404
21+
```
22+
23+
* When using multiple servers without sticky sessions, the connection can start on one server and then switch to another server. The other server is not aware of the previous connection.
24+
* Verify the client is connecting to the correct endpoint. For example, the server is hosted at `http://127.0.0.1:5000/hub/myHub` and client is trying to connect to `http://127.0.0.1:5000/myHub`.
25+
* If the connection uses the ID and takes too long to send a request to the server after the negotiate, the server:
26+
27+
* Deletes the ID.
28+
* Returns a 404.
29+
30+
### Response code 400 or 503
31+
32+
For the following error:
33+
34+
```log
35+
WebSocket connection to 'wss://xxx/HubName' failed: Error during WebSocket handshake: Unexpected response code: 400
36+
37+
Error: Failed to start the connection: Error: There was an error with the transport.
38+
```
39+
40+
This error is usually caused by a client using only the WebSockets transport but the WebSockets protocol is not enabled on the server.
41+
42+
### Response code 307
43+
44+
When using WebSockets and `skipNegotiation = true`
45+
```log
46+
WebSocket connection to 'ws://xxx/HubName' failed: Error during WebSocket handshake: Unexpected response code: 307
47+
```
48+
49+
This error can also happen during the negotiate request.
50+
51+
Common cause:
52+
* App is configured to enforce HTTPS by calling `UseHttpsRedirection` in `Startup`, or enforces HTTPS via URL rewrite rule.
53+
54+
Possible solution:
55+
* Change the URL on the client side from "http" to "https". `.withUrl("https://xxx/HubName")`
56+
57+
### Response code 405
58+
59+
Http status code 405 - Method Not Allowed
60+
61+
* The app doesn't have [CORS](xref:signalr/security#cross-origin-resource-sharing) enabled
62+
63+
### Response code 0
64+
65+
Http status code 0 - Usually a [CORS](xref:signalr/security#cross-origin-resource-sharing) issue, no status code is given
66+
67+
```log
68+
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/default/negotiate?negotiateVersion=1. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
69+
```
70+
71+
* Add the expected origins to `.WithOrigins(...)`
72+
73+
```log
74+
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/default/negotiate?negotiateVersion=1. (Reason: expected 'true' in CORS header 'Access-Control-Allow-Credentials').
75+
```
76+
77+
* Add `.AllowCredentials()` to your CORS policy. Cannot use `.AllowAnyOrigin()` or `.WithOrigins("*")` with this option
78+
79+
### Response code 413
80+
81+
Http status code 413 - Payload Too Large
82+
83+
This is often caused by having an access token that is over 4k.
84+
85+
* If using the Azure SignalR Service, reduce the token size by customizing the claims being sent through the Service with:
86+
```csharp
87+
.AddAzureSignalR(options =>
88+
{
89+
options.ClaimsProvider = context => context.User.Claims;
90+
});
91+
```
92+
93+
### Transient network failures
94+
95+
Transient network failures may close the SignalR connection. The server may interpret the closed connection as a graceful client disconnect. To get more info on why a client disconnected in those cases [gather logs from the client and server](xref:signalr/diagnostics).

aspnetcore/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@
705705
- name: Logging and diagnostics
706706
displayName: signalr
707707
uid: signalr/diagnostics
708+
- name: Troubleshooting
709+
displayName: signalr
710+
uid: signalr/troubleshoot
708711
- name: Specifications
709712
displayName: signalr, specs
710713
items:

0 commit comments

Comments
 (0)