views:

447

answers:

2

In my prism application I'm getting the error Activation error occured while trying to get instance of type CustomerModule, key \"\".

It's caused by the fact that my customers module I'm trying to inject a "menuManager" of type IMenuManager:

namespace CustomerModule
{
    public class CustomerModule : IModule
    {
        private readonly IRegionManager regionManager;
        private readonly IUnityContainer container;
        private readonly IMenuManager menuManager;

        public CustomerModule(IUnityContainer container, 
            IRegionManager regionManager, 
            IMenuManager menuManager)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.menuManager = menuManager;
        }

        public void Initialize()
        {

            container.RegisterType<IMenuManager, MenuManager>(new ContainerControlledLifetimeManager());
        ...

However, if I change the CustomerModule constructor to inject a type instead of an interface, then it works:

public CustomerModule(IUnityContainer container, 
    IRegionManager regionManager, 
    MenuManager menuManager)

So where do I need to register my MenuManager as implementing IMenuManager? It seems that registering it in CustomerModule's Initialize method is too late.

ANSWER:

I put it in ConfigureContainer() and it worked fine, be sure to leave in "base.ConfigureContainer()":

protected override void ConfigureContainer()
{
    base.ConfigureContainer();
    Container.RegisterType<MenuManager>(new ContainerControlledLifetimeManager());
}
+1  A: 

As you've probably realised that's because the constructor is being called before the initialise method.

Two solutions are:

1) Have a ShellBootstrapper class in your Shell project with a method thats called when the program loads.

Have the method in the bootstrapper register any globabl Interfaces with your container.

2) Alternatively take the IMenuManager out of the constructor and just resolve it after you've registered it.

public void Initialize()
{
    container.RegisterType<IMenuManager, MenuManager>(new ContainerControlledLifetimeManager());
    this.menuManager = container.Resolve<IMenuManager>();
}

Hope this helps!

Andy Clarke
+2  A: 

Why would you ask for a MenuManager in the same module you are registering it in?

Unless you really think your MenuManager should be something provided by an external module, you might consider putting this registration in your bootstrapper if your modules will be dependent on it. It'd be something you'd put in your ConfigureContainer method of your bootstrapper.

Anderson Imes
I got it working in GetModuleCatalog() but didn't know about ConfigureContainer, I put it in there now, thanks.
Edward Tanguay