Event not firing with IEventAggregator

Topics: Prism v4 - WPF 4
Feb 1, 2011 at 2:05 PM

Hi,

I am developing an application which loads 2 Module on 2 different region. Module A gets the data from database with list of all available applicaiton and show it to user. When user clicks on the application it publishes an event. Following is the code for it.

public void ApplicationClick(object parameter)
        {
            //Some implementation
            //MessageBox.Show(parameter.ToString() + " - In Discovery Manager");

            ApplicationInformation appInfo = new ApplicationInformation();
            appInfo.Name = parameter.ToString();

            if (!string.IsNullOrEmpty(appInfo.Name))
                _eventAggregator.GetEvent<ApplicationClickEvent>().Publish(appInfo);
        }
ApplicationClick is called via ICommand implementation. Module A also register IEventAggregator type in the container as follows
	this._container.RegisterType<IEventAggregator, EventAggregator>(new ContainerControlledLifetimeManager());

Module B in one of the ViewModel of a view subscribe to the event published by Module A as follows.

public ApplicationNameViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            this.ApplicationName = "No Applcation Loaded";

            ApplicationClickEvent applicationClickEvent = _eventAggregator.GetEvent<ApplicationClickEvent>();

            if (_subscriptionToken != null)
            {
                applicationClickEvent.Unsubscribe(_subscriptionToken);
            }

            bool keepSubscriberReferenceAlive = true;
            _subscriptionToken = applicationClickEvent.Subscribe(ApplicationClickedEventHandler, ThreadOption.UIThread, keepSubscriberReferenceAlive);

        }
Module B also register IEventAggregator as follows.
this._container.RegisterType<IEventAggregator, EventAggregator>(new ContainerControlledLifetimeManager());
Now my problem is, I am not receving any event notification on Module B, any ideas why? Am I doing anything wrong?
Feb 2, 2011 at 1:55 PM

Hi,

Take into account that is no need to register the EventAggregator in the Initialize method of your modules. The UnityBootstrapper class is already registering the EventAggregator in the container, in the ConfigureContainer method.

When you get the ApplicationClickEvent in the ApplicationClick event of moduleA to publish it, you must use the same EventAggregator instance that will be used to get ApplicationClickEvent  in moduleB. 

Since you are registering again the EventAggregator in the other module, that is probably be the reason why you are not receiving notifications in your viewmodels, as Unity is creating a different singleton instance after the new type mapping is registered. 

I hope you find this information useful. 

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

Feb 2, 2011 at 3:39 PM

Thanks for the reply.

I think EventAggregator doesn't work with MefBootStrapper implementation. Since I was using MefExtension in my main Bootstrapper class, I was not able to send the container class from Bootstrapper to ModuleA and ModuleB. Since I converted my Bootstrapper to use UnityBootstrapper all started working well.

Correct me if I am wrong, but does anyone ever implemented EventAggregator with Mef. Sample would be very helpful.

Thanks

Feb 2, 2011 at 4:46 PM

Take into account that is possible to use MEF with EventAggregator to support this functionality. Prism Library provides a MefEventAggregator class.

You might find the following thread very useful where this topic is covered.

Please, let me know if this information helps you.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic