upta / pubsub

An extremely light-weight, easy to use PCL pub/sub library
Apache License 2.0
219 stars 53 forks source link

More of a question than issue: Subscriber handles the same message multiple times #32

Open digitaldias opened 4 years ago

digitaldias commented 4 years ago

I have a piece of code that takes an "event" and stores that event to CosmosDb. I've verified that there is only a Singleton instance of my EventManagementService (constructor only runs once), and also that the publisher of the event only transmits the event once. Here is the subscriber piece of code:

public void Initialize()
        {            
            _hub.Subscribe<NoobEvent>(SaveMessageToEventStore);            
        }

        private async void SaveMessageToEventStore(NbtEvent updateEvent)
        {
            _hub.UnSubscribe<NoobEvent>();

            _logger.Information($"Transmitting event to eventstore: {updateEvent.EventName} {updateEvent.EventType}");

            //TODO: (Pedro) Validate the updateEvent

            await _exceptionHandler.RunAsync(() => _eventStore.Write(updateEvent));

            _hub.Subscribe<NoobEvent>(SaveMessageToEventStore);
        }

If I do not unsubscribe while executing the "SaveMessageToEventstore()", then I see that it executes twice. Questions to this:

In advance, thanks.

upta commented 4 years ago

This definitely smells like a bug, I'll have to dig into it a bit and see what I can find