Having problem injecting eventaggregator into viewmodel

Topics: Prism v4 - Silverlight 4
Sep 16, 2011 at 10:29 AM
Hi all
Looking for some help please.  I'm going around in circles at the moment.  I'm trying to inject an eventaggregator into a viewmodel and am getting the error below.
An exception has occurred while trying to add a view to region 'WorkRegion'. 
    - The most likely causing exception was was: 'System.InvalidOperationException: The current type, Microsoft.Practices.Prism.Events.IEventAggregator, is an interface and cannot be constructed. Are you missing a type mapping?
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context)
   at BuildUp_Microsoft.Practices.Prism.Events.IEventAggregator(IBuilderContext )
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)'.
    But also check the InnerExceptions for more detail or call .GetRootException().
My bootstrapper is as follows:
public class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            Shell shell = Container.Resolve<Shell>();
            Application.Current.RootVisual = shell;
            return shell;
        }
        protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
        {
            return new ModuleCatalog()
                .AddModule(typeof(RouteInfoModule.ViewRouteInfoModule));
        }
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
	//Not sure if i need to register this below, but either way it still won't work	
            IEventAggregator eventAggregator = new EventAggregator();
            Container.RegisterInstance<IEventAggregator>(eventAggregator);
        }
    }
My module is as follows:
	
public class ViewRouteInfoModule : IModule
    {
        public IUnityContainer _container { getset; }
        public IRegionManager _regionManager { getset; }
 
        public ViewRouteInfoModule(IUnityContainer container, IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;
        }
 
        public void Initialize()
        {
            _regionManager.RegisterViewWithRegion("MainRegion"typeof(RouteInfo));
            _regionManager.RegisterViewWithRegion("WorkRegion"typeof(ScheduledWork));  <- this is where the error occurs as viewmodel expects eventaggregator 
        }
    }
ViewModel as follows(cut):
public ScheduledWorkViewModel(IEventAggregator eventAggregator)
		{      
			try
			{
                _eventAggregator = eventAggregator;
I've been looking at this for a while and have tried various examples without success.  Please let me know if i need to supply more info.
Thanks
Paul
Developer
Sep 16, 2011 at 2:33 PM

Hi,

Even though the exception you're mentioning does imply that you don't have the Event Aggregator registered in the container, this is one of the services registered into the container by default in the ConfigureContainer method of the UnityBootstrapper. Therefore, there's no need to register it again when overriding that method from your bootstrapper.
So far, we couldn’t find anything wrong within the code snippets that you posted. Hence, it would be helpful if you could provide us with a repro sample application that portrays that problem, so we can help you find the cause and a possible workaround for your issue.

Regards,

Agustin Adami
http://blogs.southworks.net/aadami

Sep 16, 2011 at 4:02 PM

Hi Agustin

Thanks for the quick response.  I didn't think i had to have the event aggregator registered but had tried everything else.  I'm coming to the conclusion that it must have something to do with the dll's that i have registered. 

Can you tell me which dll's i should be using for my silverlight app with prism.  I see references to Microsoft.Practices.Composite dll's, is this correct?

Thanks

Paul

Developer
Sep 16, 2011 at 4:44 PM

Hi,

Based on my understanding of your scenario, you should reference the Microsoft.Practices.Prism assembly which is the one provided in Prism v4. If you don´t find it in the Global Assembly Cache try running the RegisterPrismBinaries.bat file within Prism v4. This will register Prism v4 assemblies in the Global Assembly Cache. Note that the Microsoft.Practices.Composite is present in Prism v2, not in v4.

I hope you find this useful.

Agustin Adami
http://blogs.southworks.net/aadami

Sep 17, 2011 at 2:46 PM

RegisterPrismBinaries.bat does not put Prism in the GAC.  It only adds registry entries so that Visual Studio will list the Prism and Unity assemblies in the Add References dialog.

Karl

Sep 19, 2011 at 7:51 AM

Thanks all

Developer
Sep 19, 2011 at 1:10 PM
Edited Sep 19, 2011 at 1:35 PM

Karl,

Thank you very much for the correction. I am sorry for the confusion generated with this.

Regards. 

Agustin Adami
http://blogs.southworks.net/aadami