EventAggregator Dropping Events

Topics: Prism v2 - WPF 3.5
Apr 16, 2009 at 4:51 PM
Hi there,

I've been working through the examples in the latest screen cast on Channel 9 (http://channel9.msdn.com/posts/akMSFT/Creating-a-modular-application-using-Prism-v2-Screencast-44--Decoupled-Communication/) which is really very helpful, thanks.

However, when hooking up a Pub/Sub setup with the EventAggregator between two modules, the system stops recieving events on the subscribe side at a seemingly random inverval.  Sometimes it'll fires successfuly 10 times, sometines once only, sometimes 0.

I was figuring that something might have been getting garbage collected (hence the random interval of working) and then was no longer available to fire the events (total speculation, I have no idea).

Anyhow, the wierd thing is when I was trying to debug this theory, I put a force GC.Collect() in the subscribing module [see code below] and the problem disappears.  I wonder if anyone can point out what I'm doing wrong [see code below], or is this a bug in the framework ?

- Modules loaded via config file.
(I can post more of the sample project if necessary)

--------------------- Publishing Module ---------------------
        private readonly IEventAggregator eventAggregator;

        public ChildTwoViewModel(IChild2View view, IChild2Model model, IEventAggregator eventAggregator)
            this.eventAggregator = eventAggregator;
            PublishEventCommand = new DelegateCommand<string>(param => OnPublishEvent(), param => true);

        private void OnPublishEvent()
            var value = SelectedString;
            if (string.IsNullOrEmpty(value)) value = "<Nothing Selected>";

--------------------- Subscribing Module ---------------------
        public ChildOnePresenter(IChild1View view, IChild1Service model, IEventAggregator eventAggregator)

            GC.Collect(); //TEMP - when garbage is collected events work consistently, when not then drop randomly after 1 - 15 invokations.

        public void HandleSubscribedEvent(string value)
            Debug.WriteLine("Subscribed Event Recieved");

Thanks a lot!

Apr 17, 2009 at 9:34 PM



I have tried to reproduce your issue with no success. However, a possible scenario where this could happen is if Child1View  does not hold any strong reference to the presenter. If that is the case the presenter could be being Garbage Collected and thus the events wouldn’t be handled. Writing the GC.Collect(); could be doing something different in different environments/applications, so that is why the Garbage Collector might be preventing the presenter to be collected in your environment.


To workaround this scenario you could hold a reference to your presenter in your view or modify your presenter to be a ViewModel (hence keeping a reference to it from the View by design).


Please let me know if this helps.


Damian Schenkelman

Apr 17, 2009 at 9:36 PM
Thanks Damian - I'll check that out, and report back if it's not the weak-reference to the presenter issue.
Really appreciate you taking the time to look at this!