DependencyResolver not considering already loaded modules

Nov 6, 2008 at 8:25 AM
Hi all,

I'm currently working on a service which based on a configuration loads modules dynamically. After retrieving this information I load each module for that specific configuration. The dynamically loaded modules can have dependencies on other already loaded modules. The problem with that is that the ModuleLoaded (or the DependencyResolver) doesn't consider already loaded (initialized) modules and therefore throws and exception saying:

"A module declared a dependency on another module which is not declared to be loaded. Missing module(s): ..."

If I don't specify dependencies it works fine.

Any ideas, how to get around that?

Nov 7, 2008 at 12:56 PM

Hi Chris,


A possible way to solve that issue is by creating an extension method to the Module Enumerator like the following one:

using System.Linq;

using System.Collections.Generic;


// The namespace is important to make the extension method work.

namespace Microsoft.Practices.Composite.Modularity


public static class ModuleEnumeratorExtensions


public static ModuleInfo[] GetModuleWithDependencies(this IModuleEnumerator moduleEnumerator, string moduleName)


List<ModuleInfo> moduleInfoList = new List<ModuleInfo>();

ModuleInfo module = moduleEnumerator.GetModule(moduleName);


if (module.DependsOn != null)


foreach (string dependencyName in module.DependsOn)


if (!moduleInfoList.Exists(existingModule => existingModule.ModuleName == dependencyName))


moduleInfoList.AddRange(GetModuleWithDependencies(moduleEnumerator, dependencyName));




return moduleInfoList.ToArray();




So you could then initialize a module on demand in the following way:

moduleLoader.Initialize( moduleEnumerator.GetModuleWithDependencies("MyOnDemandModule"));


However, it is not necessary to add dependencies to modules loaded at startup from modules loaded on demand, because they will be loaded always (if one of them fails to be loaded, the ModuleLoader will throw a ModuleLoadException exception).


Please let me know if this helps.


Mariano Converti

Nov 7, 2008 at 6:15 PM
Hi Mariano,

you are right about not setting dependencies on start-up loaded modules it came to my mind after thinking a bit more about the reason why the implementation is done that way. So I went with that and I'm now forcing everyone to have his dependencies on start-up loaded modules stated.

Nice extension to the ModuleEnumerator, I was thinking about creating my own enumerator but that is even better!