简体   繁体   中英

Can't use websocket with SignalR when hosting service on azure windows VM (C#/.NET5.0)

I have a .NET5.0 standalone service (which is installed as a windows service) which starts a basic SignalR hub. It uses Http.Sys internally has some other API controllers, and I'm currently connecting to it from a WinForms client using HubConnection.

It works perfectly on my development machine, but when I move it into an Azure VM (server 2019 datacentre) the first WebSocket call from the client hangs for a while, then fails. It then seems to "work" from then on, but very, very slowly (too slow to use).

I have opened the appropriate ports incoming and outgoing on the Azure server, and in Azure portal.

If I run my Winforms client on a different azure VM in the same virtual network, it all works fine. Therefore, I am sure the server works correctly, it only fails when I come in over the internet.

The server logfile shows this when I try to connect externally:

2021-11-24 17:10:19.6884|1|DEBUG|Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher|Received hub invocation: InvocationMessage { InvocationId: "1", Target: "GetAllState", Arguments: [ ], StreamIds: [ ] }. 17:10:31.1564|4|DEBUG|Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport|Waiting for the application to finish sending data. 2021-11-24 17:10:31.1564|12|DEBUG|Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport|Error writing frame. System.Threading.Tasks.TaskCanceledException: A task was canceled. at System.Net.WebSockets.ManagedWebSocket.SendFrameFallbackAsync(MessageOpcode opcode, Boolean endOfMessage, ReadOnlyMemory 1 payloadBuffer, CancellationToken cancellationToken) at System.Net.WebSockets.WebSocketExtensions.SendMultiSegmentAsync(WebSocket webSocket, ReadOnlySequence 1 buffer, WebSocketMessageType webSocketMessageType, CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsServerTransport.StartSending(WebSocket socket) 2021-11-24 17:10:31.1564|2|DEBUG|Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport|Socket closed.

The client shows this:

2021-11-24 18:55:43.4679|50|ERROR|Microsoft.AspNetCore.SignalR.Client.HubConnection|The server connection was terminated with an error. System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.. ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.

I have looked at so many articles trying to fix this but I can't find anything relevant. It must be config-related, and it must be at "azure portal" level because the service works across the LAN. Does anyone have any idea what I need to do to get this running?

Update:

I created a couple of console apps using TcpListener and TcpClient on a new custom port, ran the server app on my Azure VM, connected to it successfully across the internet, and passed messages between the two. Very confused as to why the WebSocket fails to work over the internet.

Update2:

I changed the service to use Kestrel and it now works as expected. So, for whatever reason it refuses to work over the internet using Http.Sys. I was only using Http.Sys to have basic auth for some other clients that won't be using this service so Kestrel will work fine.

I changed the service to use Kestrel and it now works as expected. So, for whatever reason it refuses to work over the internet using Http.Sys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM