Blazor Server客户端与SignalR不起作用。

huangapple go评论69阅读模式
英文:

Blazor Server client side with SignalR doesn't work

问题

我们的Blazor服务器应用程序存在问题。我们的应用程序全天显示实时数据。如果我们运行它几个小时,就不会出现问题,但在长时间测试后,例如10个小时或更长时间后,客户端会停止工作(有时不会在页面之间切换,有时停止通过SignalR刷新数据)。

如果我们关闭并重新打开浏览器页面,应用程序就会恢复正常工作。在我们的日志和Windows事件查看器中没有任何错误。

我们在Visual Studio 2022调试模式和应用程序发布后的IIS中都有相同的行为。我该如何解决这个问题?

  1. 在客户端端降低内存使用。
  2. 使用SignalR实现OnDisconnect以删除非活动连接。
  3. 将IIS的空闲超时设置为0,以永远不关闭与客户端的连接。
英文:

We have a problem with a Blazor server application. Our application shows real time data all day. If we run that for few hours, we haven't a problem, but during a long run test, after 10 hours or more, the client side stops to work (sometimes no switch between pages, sometimes stop refresh data with SignalR).

If we close and reopen the browser page, the application starts to work again. There isn't any error in our logs and in the Windows Event Viewer.

We have the same behavior in Visual Studio 2022 debug mode and in IIS after application publish.
How can I fix this problem?

  1. Reduced memory usage in the client side.
  2. Implemented OnDisconnect with SignalR for delete inactive connections.
  3. Set IIS idle-time out to 0 for never close connections with clients

答案1

得分: 1

以下是要翻译的内容:

这里有一些进一步排除故障的想法。问题在于浏览器和Blazor服务器之间的SignalR连接断开了。当这种情况发生时,浏览器将无法接收页面DOM的更新,您会遇到“无法在页面之间切换”的问题。如果您正在本地调试Blazor服务器,可以通过等待在调试断点处来复现SignalR断开连接的情况。

  1. 当出现此错误时,请检查浏览器的开发工具(DevTools)。如果浏览器失去SignalR连接,JS控制台中应该会出现异常。这将确认SignalR断开连接,并可能提供根本原因的提示(例如,在您的.razor代码中可能存在未处理的应用程序异常)。

  2. 考虑使用JS手动建立SignalR连接。这个SO线程/答案提供了如何在Blazor Server中使用JS代码进行设置的一些信息。这种方法可以让您有机会检测(来自浏览器/客户端的)断开连接,并从浏览器自动重新连接。

  3. 考虑使用托管的Azure SignalR服务以获得更好的连接性。请注意,如果这些是根本原因,您可以配置Circuit和Hub超时选项。

  4. 实现一个CircuitHandler来跟踪Blazor Server上的SignalR连接。这将有助于监视服务器上的SignalR连接。您可以使用此来识别何时在服务器端丢失连接,然后可能触发页面刷新以重新连接。

最后,请考虑浏览器和您的Blazor服务器之间的SignalR连接可能中断了。例如,一些用户无法通过VPN连接访问Blazor服务器应用程序,因为SignalR/WebSocket连接中断了。有可能IIS设置了一些影响您的应用程序的超时。

英文:

Here are some ideas to further troubleshoot. The issue is that the SignalR connection between the browser and Blazor Server is becoming disconnected. When this happens, the browser will not receive updates to the page DOM and you experience "no switch between pages". If you are debugging Blazor Server locally, you can reproduce a SignalR disconnection by waiting at a debug breakpoint.

  1. Inspect browser DevTools when this error happens. If the browser loses the SignalR connection, there should be an exception in the JS Console. This will confirm the SignalR disconnection and possibly provide a hint of the root cause (ie, possible unhandled app exception in your .razor code)

  2. Consider establishing the SignalR connection manually using JS. This SO thread/answer has some info on how to set this up for Blazor Server using JS code. This approach gives you a chance to detect the disconnection (from the browser/client) and automatically reconnect from the browser.

  3. Consider using a hosted Azure SignalR Service for better connectivity. Note that you can configure the Circuit and Hub timeout options if those are the root cause.

  4. Implement a CircuitHandler to track the SignalR connections on your Blazor Server. This will help monitor the SignalR connections from the server. You can use this to identify when you lose connections on the server side and possibly trigger a page refresh to reconnect.

Lastly, consider that the SignalR connection may be broken between the browser and your Blazor Server. For example, some users can't access a Blazor Server app over VPN connections, because the SignalR/WebSocket connection is broken. It's possible that IIS has some timeout that is affecting your app.

huangapple
  • 本文由 发表于 2023年6月27日 18:28:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定