c# - 在 azure windows VM (C#/.NET5.0) 上托管服务时,无法将 websocket 与 SignalR 结合使用

标签 c# azure asp.net-core websocket signalr

我有一个 .NET5.0 独立服务(作为 Windows 服务安装),它启动基本的 SignalR 集线器。它使用 Http.Sys,内部有一些其他 API Controller ,我目前正在使用 HubConnection 从 WinForms 客户端连接到它。

它在我的开发计算机上完美运行,但是当我将其移动到 Azure VM(服务器 2019 数据中心)时,来自客户端的第一个 WebSocket 调用会挂起一段时间,然后失败。然后它似乎从那时起就“工作”了,但是非常非常慢(太慢而无法使用)。

我已在 Azure 服务器和 Azure 门户中打开相应的传入和传出端口。

如果我在同一虚拟网络中的不同 azure VM 上运行我的 Winforms 客户端,则一切正常。因此,我确信服务器工作正常,只有当我通过互联网进入时才会失败。

当我尝试外部连接时,服务器日志文件显示以下内容:

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, ReadOnlyMemory1 payloadBuffer, CancellationToken cancellationToken) at System.Net.WebSockets.WebSocketExtensions.SendMultiSegmentAsync(WebSocket webSocket, ReadOnlySequence1 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.

我看过很多文章试图解决这个问题,但我找不到任何相关的内容。它必须与配置相关,并且必须处于“azure 门户”级别,因为该服务跨 LAN 工作。有谁知道我需要做什么才能让它运行?

更新:

我在新的自定义端口上使用 TcpListener 和 TcpClient 创建了几个控制台应用程序,在我的 Azure VM 上运行服务器应用程序,通过 Internet 成功连接到它,并在两者之间传递消息。非常困惑为什么 WebSocket 无法在互联网上工作。

更新2:

我将服务更改为使用 Kestrel,现在它可以按预期工作。因此,无论出于何种原因,它都拒绝使用 Http.Sys 在互联网上工作。我只是使用 Http.Sys 对其他一些不会使用此服务的客户端进行基本身份验证,因此 Kestrel 可以正常工作。

最佳答案

我将服务更改为使用 Kestrel,现在它可以按预期工作。因此,无论出于何种原因,它都拒绝使用 Http.Sys 在互联网上工作。

关于c# - 在 azure windows VM (C#/.NET5.0) 上托管服务时,无法将 websocket 与 SignalR 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70102638/

相关文章:

c# - 找不到模块 'eslint-config-defaults/configurations/eslint'

azure - 关于调用一个实体的多个编排功能的问题

socket.io - Asp.net MVC Core 可以与 Socket.io JS 库一起使用吗?

c# - 关于泛型和 IEnumerable 的方法重载解析

c# - 取出除最后一个满足条件的所有项目?

C# SMTP 套接字问题

azure - 在azure文件共享中创建目录结构

azure - 如何捕获从事件中心到 azure 流分析的错误 json 记录

c# - 用户首次使用 OpenId Connect 登录后,将新 UserId 放入数据库的位置在哪里?

c# - LinkGenerator 返回 NULL Url 而 Url.Action 不...为什么