3

Atm in my application I do like this:

class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<Shell>();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();

            App.Current.MainWindow = (Window)Shell;
            App.Current.MainWindow.Show();
        }     

        protected override void ConfigureModuleCatalog()
        {            
            base.ConfigureModuleCatalog();
            var moduleCatalog = (ModuleCatalog)ModuleCatalog;

            moduleCatalog.AddModule(typeof(FooModule));
            moduleCatalog.AddModule(typeof(BarModule));
        }        
    }

I would like to load FooModule and BarModule by indicating the path to the dll file, something like this:

protected override void ConfigureModuleCatalog()
{
...
            var assembly = Assembly.LoadFrom(@"libs\FooLib.dll");
            var type = assembly.GetType("FooLib.FooModule");
            moduleCatalog.AddModule(type);
...
}

but it doesn't work, I get this error message on Bootstrapper.Run() :

There is currently no moduleTypeLoader in the ModuleManager that can retrieve the specified module.

EDIT: this is my FooModule:

public class FooModule : IModule
    {
        private readonly IRegionViewRegistry _regionViewRegistry;

        public FooModule(IRegionViewRegistry registry)
        {
            _regionViewRegistry = registry;
        }

        public void Initialize()
        {
            _regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Main));
        }
    }
Omu
  • 69,856
  • 92
  • 277
  • 407

3 Answers3

3

Ok, try to make your ConfigureModuleCatalog looking like this:

protected override void ConfigureModuleCatalog()
{
    string path = @"libs\FooLib.dll";
    var assembly = Assembly.LoadFrom(path);
    var type = assembly.GetType("FooLib.FooModule");
    ModuleCatalog.AddModule(new ModuleInfo
                                {
                                    ModuleName = type.Name,
                                    ModuleType = type.AssemblyQualifiedName,
                                    Ref = new Uri(path, UriKind.RelativeOrAbsolute).AbsoluteUri
                                });


}

The key thing is:

Ref = new Uri(path, UriKind.RelativeOrAbsolute).AbsoluteUri    

prism checks whether Ref property refers to physical file(file://) and loads assembly from this file.

alex.b
  • 4,547
  • 1
  • 31
  • 52
1

One easier method not to enter path manually is, obtain it from type->assembly->location

    Type module1Type = typeof(Module1.Module1);
    string path = module1Type.Assembly.Location;
    moduleCatalog.AddModule(
      new ModuleInfo()
      {
          ModuleName = module1Type.Name,
          ModuleType = module1Type.AssemblyQualifiedName,
          Ref = new Uri(path, UriKind.RelativeOrAbsolute).AbsoluteUri
      });

    return moduleCatalog;
freewill
  • 1,111
  • 1
  • 10
  • 23
1

I think Prism v4 Loading modules on demand with DirectoryModuleCatalog could help.

UPDATE:
Sorry, just realized that reference mentioned above won't help.
Try this one from msdn - "Loading Modules on Demand" section, I think that's what you need.

alex.b
  • 4,547
  • 1
  • 31
  • 52
  • no, that doesn't help, this way they aren't loaded at all, or not demanded I guess – Omu Apr 02 '12 at 10:44
  • Have you checked whether your `FooModule` and `BarModule` could be via Reflection loaded and instantiated at all? Just create some minimal test console application and try to check it. – alex.b Apr 02 '12 at 11:33
  • And you can check [this thread on codeplex](http://compositewpf.codeplex.com/discussions/58512), maybe it could help – alex.b Apr 02 '12 at 11:35
  • when I try to do Activator.CreateInstance(type) I get "No parameterless constructor defined for this object" – Omu Apr 02 '12 at 12:10
  • That's the problem. So add parameterles constructor `FooModule` and `BarModule`. Please let us know in case of any objectives or problems with this. – alex.b Apr 02 '12 at 13:49
  • I've edited my question to show the FooModule, as you can see there is a parameter in the constructor which is needed, is it possible to achieve the same without the parameter ? – Omu Apr 02 '12 at 13:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9598/discussion-between-aleksey-berezan-and-chuck-norris) – alex.b Apr 02 '12 at 14:13
  • I wrote in the chat: in the app that you did you referenced the ModuleC, which you weren't supposed to, this is why you app was working, if you remove reference to ModuleC you will also get that error – Omu Apr 03 '12 at 09:20