It looks like the problem is that when the Module is registered to the container it isn't registered as a specific instance, so when you request the resolve a new instance is created. I've had a look through the CAL source code and the registering happens in the middle of a convoluted set of method calls, so I doubt you'll be able to sensibly supply your own implementation.
This means you'll need to register the instance of your module somewhere else. The way to do this off the top of my head is to create some kind of module instance catalog and add a call to each of the module's initilize method to register with it.
public class ModuleInstanceCatalog : IModuleInstanceCatalog
{
private Dictionary<Type, ModuleBase> _modules = new Dictionary<Type, ModuleBase>();
public void RegisterModuleInstance(ModuleBase module)
{
_modules.Add(module.GetType(), module);
}
public ModuleBase GetModuleInstanceByType(Type type)
{
return _modules[type];
}
}
In your Bootstrapper CreateShell():
this.Container.RegisterInstance<IModuleInstanceCatalog>(new ModuleInstanceCatalog());
In each module's Initilize():
IModuleInstanceCatalog catalog = this.Container.Resolve<IModuleInstanceCatalog>();
catalog.RegisterModuleInstance(this);
Then replace the code in your question with:
IModuleCatalog moduleCatalog = this.Container.Resolve<IModuleCatalog>();
IModuleInstanceCatalog instanceCatalog = this.Container.Resolve<IModuleInstanceCatalog>();
moduleCatalog.Modules.ToList().ForEach(m =>
{
instanceCatalog.GetModuleInstanceByType(m.ModuleType).ApplySecurity();
});
I'm not overly happy with this as it feels like a bit of a fudge, but it should work for what you want.