Pregunta

I have a notification center which is a singelton that runs in the background every few seconds and fetch new notifications from DB, if there are subscribres to his event, he trigger the event with the new notifications.

On the other side I have a signalR clients that subscribe to this event like this:

public void Subscribe()
        {
           NotificationCenter.OnNotificationArrived += PublishNotifications;
        }

On the same hub I have this method that removes the subscription:

public override System.Threading.Tasks.Task OnDisconnected()
        {
            NotificationCenter.OnNotificationArrived -= PublishNotifications;
            return base.OnDisconnected();
        }

The PublishNotifications method also lies in the same hub.

When I debug my code, I can see that the event is null at first. After the first subscription, the event holds one method. After another subscription the event holds 2 methods. After removing the subscriptions the event still holds 2 methods.

Any idea what am I doing wrong?

btw: I am using signalr 1.2

¿Fue útil?

Solución

I think I learned an important lession that should be shared with other SignalR newbies.

I tought that when SignalR creates connection between the browser and the server, it also creates Hub instance and hold it for the whole session, I was wrong. It looks like SignalR holds the connection under the hood and every time the client calls the Hub, new Hub instance created and disposed at the end of the request. This is why I couldn't unsubscribe from the notification center.

All I had to do is unsubscribe from notification center via the same instance that was hooked and running in the background.

Thanks Jon for the enlightment!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top