dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.27k stars 9.96k forks source link

SignalR client error: The server disconnected before the handshake could be started. #55268

Closed mxfgithub123 closed 5 months ago

mxfgithub123 commented 5 months ago

Is there an existing issue for this?

Describe the bug

Hy,

I am trying to make a connection between my client application with the server. And whenever I try to connect to the server to connect to the signalr service. Always give me this error: "The server disconnected before the handshake could".

Env: Client: win11 23H2(22631.3447) Server: Windows Server 2019 version: 1809 17763.5576 Other: .net sdk8.0.4

Log:

dbug: Microsoft.AspNetCore.SignalR.Client.HubConnection[40]
      Registering handler for client method 'ReceiveWelcome'.
dbug: Microsoft.AspNetCore.SignalR.Client.HubConnection[40]
      Registering handler for client method 'SendReceiveAuditNotify'.
dbug: Microsoft.AspNetCore.SignalR.Client.HubConnection[40]
      Registering handler for client method 'ReceivedMessage'.
dbug: Microsoft.AspNetCore.SignalR.Client.HubConnection[41]
      Starting HubConnection.
dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[1]
      Starting HttpConnection.
dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[8]
      Establishing connection with server at 'http://tapi.owlyz.com/hub/onlineUser'.
dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[9]
      Established connection 'xJP0U_FYjhXr9Bug7U7eHQ' with the server.
dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[7]
      Starting transport 'WebSockets' with Url: http://xxxx.com/hub/onlineUser.
info: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[1]
      Starting transport. Transfer mode: Text. Url: 'ws://xxxx.com/hub/onlineUser?id=Ir9ScYIgbxFmL2WY79x9ng'.
fail: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[11]
      Failed to start connection. Error starting transport 'WebSockets'.
      System.Net.WebSockets.WebSocketException (0x80004005): The server's response was missing the required header 'Connection'.
         at System.Net.WebSockets.WebSocketHandle.ValidateHeader(HttpHeaders headers, String name, String expectedValue)
         at System.Net.WebSockets.WebSocketHandle.ValidateResponse(HttpResponseMessage response, String secValue)
         at System.Net.WebSockets.WebSocketHandle.ConnectAsync(Uri uri, HttpMessageInvoker invoker, CancellationToken cancellationToken, ClientWebSocketOptions options)
         at System.Net.WebSockets.ClientWebSocket.ConnectAsyncCore(Uri uri, HttpMessageInvoker invoker, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport.DefaultWebSocketFactory(WebSocketConnectionContext context, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport.StartAsync(Uri url, TransferFormat transferFormat, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartTransport(Uri connectUrl, HttpTransportType transportType, TransferFormat transferFormat, CancellationToken cancellationToken, Boolean useStatefulReconnect)
dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[15]
      Skipping transport WebSockets because it failed to initialize.
      System.Net.WebSockets.WebSocketException (0x80004005): The server's response was missing the required header 'Connection'.
         at System.Net.WebSockets.WebSocketHandle.ValidateHeader(HttpHeaders headers, String name, String expectedValue)
         at System.Net.WebSockets.WebSocketHandle.ValidateResponse(HttpResponseMessage response, String secValue)
         at System.Net.WebSockets.WebSocketHandle.ConnectAsync(Uri uri, HttpMessageInvoker invoker, CancellationToken cancellationToken, ClientWebSocketOptions options)
         at System.Net.WebSockets.ClientWebSocket.ConnectAsyncCore(Uri uri, HttpMessageInvoker invoker, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport.DefaultWebSocketFactory(WebSocketConnectionContext context, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport.StartAsync(Uri url, TransferFormat transferFormat, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartTransport(Uri connectUrl, HttpTransportType transportType, TransferFormat transferFormat, CancellationToken cancellationToken, Boolean useStatefulReconnect)
         at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.SelectAndStartTransport(TransferFormat transferFormat, CancellationToken cancellationToken)
dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[8]
      Establishing connection with server at 'http://xxxx.com/hub/onlineUser'.
dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[9]
      Established connection '2NNA1vZ_4FZwhnkAqveq3A' with the server.
dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[7]
      Starting transport 'ServerSentEvents' with Url: http://xxxx.com/hub/onlineUser.
info: Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport[1]
      Starting transport. Transfer mode: Text.
dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport[3]
      Starting receive loop.
dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport[9]
      Received 49 bytes. Parsing SSE frame.
dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport[7]
      Passing message to application. Payload size: 36.
dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport[4]
      Receive loop stopped.
dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport[100]
      Starting the send loop.
dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[18]
      Transport 'ServerSentEvents' started.
dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport[102]
      Send loop canceled.
info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3]
      HttpConnection Started.
dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport[101]
      Send loop stopped.
info: Microsoft.AspNetCore.SignalR.Client.HubConnection[24]
      Using HubProtocol 'json v1'.
dbug: Microsoft.AspNetCore.SignalR.Client.HubConnection[28]
      Sending Hub Handshake.
fail: Microsoft.AspNetCore.SignalR.Client.HubConnection[35]
      The underlying connection closed while processing the handshake response. See exception for details.
      System.IO.IOException: The server disconnected before the handshake could be started.
fail: Microsoft.AspNetCore.SignalR.Client.HubConnection[43]
      Error starting connection.
      System.IO.IOException: The server disconnected before the handshake could be started.
         at Microsoft.AspNetCore.SignalR.Client.HubConnection.HandshakeAsync(ConnectionState startingConnectionState, Int32 protocolVersion, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore(CancellationToken cancellationToken)
dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[4]
      Disposing HttpConnection.
info: Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport[6]
      Transport is stopping.
dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport[2]
      Transport stopped.
info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[6]
      HttpConnection Disposed.

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

mxfgithub123 commented 5 months ago

Additional information:

  1. Debugging locally works fine, but the issue arises when publishing to the server.
  2. After researching online, I found that the problem might be caused by the WebSocket module not being enabled. However, upon investigation, I confirmed that the WebSocket module is installed on the server's IIS. My IIS version is 10.0.17763.1. 924df3b25cc7ccdcc297f71c543c80f
BrennanConroy commented 5 months ago

Something is wrong with your server setup or a proxy in front of the server. You can see that not just WebSockets fails, but also ServerSentEvents. Try LongPolling and see if that has a similar issue.

mxfgithub123 commented 5 months ago

Thank you very much for your reply. Through further investigation of the server settings, I uninstalled the server protection software, and then the issue was resolved.