Search code examples
c#.netprismprism-4

load Prism modules from dll files (at startup)


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));
        }
    }

Solution

  • 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.