How to close connections when cycling a SignalR hu

2019-08-08 12:42发布

I have a SignalR HUB that maintains an in-memory repository of the various connected clients, which contains some data that we use to process requests. Ideally, these clients will stay connected all the time.

The problem I have is when we need to push a patch to the hub, we publish it, and recycle the server. This occurs quickly enough that the clients still maintain their connection, but since the website has been recycled, the memory used by the hub has been cleared and the repository no longer exists.

Maybe this is a design flaw on my part, but is there a way to force the connections to drop when cycling the server, or should I be using something other than a simple in-memory singleton for my repository? Or am I misunderstanding what's happening here altogether?

标签: iis signalr
2条回答
Melony?
2楼-- · 2019-08-08 12:45

You can use something similar to what is posted here : OnDisconnect and Logged on Users after recycle?

I had the same problem and what I did is :

1-make all clients register a 'ForceRestart' function :

_proxy.On("ForceRestart", () => _hub.Stop());

2-so in the server constructor, when the number of connected clients (I was tracking in memory) is zero, it means server has been recycled, or server is brand new, so I call:

IHubContext _hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
_hubContext.Clients.All.ForceRestart();

This way I clean up all remaining dummy connections. I hope this helps.

查看更多
smile是对你的礼貌
3楼-- · 2019-08-08 12:53

There are lots of good information at signalr documentation. Check "server disconnection scenarios" part of this

查看更多
登录 后发表回答