|
| 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). |
0 commit comments