PrismLibrary/Prism · error · ModularityException
Resources.StartupModuleDependsOnAnOnDemandModule (formatted…
Error message
Resources.StartupModuleDependsOnAnOnDemandModule (formatted with moduleInfo.ModuleName)
What it means
ValidateDependenciesInitializationMode throws ModularityException with Resources.StartupModuleDependsOnAnOnDemandModule when a module set to load WhenAvailable (at startup) depends on a module set to OnDemand. The dependency could not be initialized when the startup module needs it, so Prism rejects the configuration up front.
Solutions
- Change the OnDemand dependency module to WhenAvailable so it initializes before its dependents.
- Alternatively change the dependent startup module to OnDemand so it loads later with its dependency.
- Remove the dependency if it is not actually required.
Example fix
// before
new ModuleInfo { ModuleName = "Core", InitializationMode = InitializationMode.OnDemand },
new ModuleInfo { ModuleName = "Shell", DependsOn = new[] { "Core" }, InitializationMode = InitializationMode.WhenAvailable }
// after
new ModuleInfo { ModuleName = "Core", InitializationMode = InitializationMode.WhenAvailable },
new ModuleInfo { ModuleName = "Shell", DependsOn = new[] { "Core" }, InitializationMode = InitializationMode.WhenAvailable } Defensive patterns
Strategy: validation
Validate before calling
foreach (var m in modules.Where(m => m.InitializationMode == InitializationMode.WhenAvailable))
{
foreach (var dep in modules.Where(d => m.DependsOn?.Contains(d.ModuleName) == true))
{
if (dep.InitializationMode == InitializationMode.OnDemand)
throw new InvalidOperationException($"{m.ModuleName} (WhenAvailable) depends on OnDemand module {dep.ModuleName}");
}
} Try / catch
try
{
catalog.Validate();
}
catch (ModularityException ex)
{
logger.LogError(ex, "Startup module depends on an OnDemand module: {0}", ex.Message);
// align InitializationMode values before retrying
} Prevention
- Keep transitive dependencies of startup modules on WhenAvailable
- Review InitializationMode changes for downstream dependents
- Encode the initialization-mode graph in a startup unit test
When it happens
Trigger: Module A has InitializationMode.WhenAvailable and its DependsOn chain includes a module with InitializationMode.OnDemand; detected during catalog Validate().
Common situations: Changing a dependency to OnDemand to speed startup without checking which startup modules depend on it; conflicting InitializationMode values set in config by different teams.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Resources.ModuleDependenciesNotMetInGroup (formatted with…
- Value cannot be null. (Parameter 'name')
- Value cannot be null. (Parameter 'dependsOn')
- Value cannot be null. (Parameter 'type')
- Value cannot be null. (Parameter 'moduleInitializer')
AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15).
Data as JSON: /api/errors/ea4bc7526eb313ea.
Report an issue: GitHub.
Appendix: source
Thrown at src/Prism.Core/Modularity/ModuleCatalogBase.cs:328
ValidateDependencies(GrouplessModules.Union(group));
}
}
/// <summary>
/// Ensures that there are no modules marked to be loaded <see cref="InitializationMode.WhenAvailable"/>
/// depending on modules loaded <see cref="InitializationMode.OnDemand"/>
/// </summary>
protected virtual void ValidateDependenciesInitializationMode()
{
var moduleInfo = Modules.FirstOrDefault(
m =>
m.InitializationMode == InitializationMode.WhenAvailable &&
GetDependentModulesInner(m)
.Any(dependency => dependency.InitializationMode == InitializationMode.OnDemand));
if (moduleInfo != null)
{
throw new ModularityException(
moduleInfo.ModuleName,
string.Format(CultureInfo.CurrentCulture, Resources.StartupModuleDependsOnAnOnDemandModule, moduleInfo.ModuleName));
}
}
/// <summary>
/// Returns the <see cref="IModuleInfo"/> on which the received module depends on.
/// </summary>
/// <param name="moduleInfo">Module whose dependant modules are requested.</param>
/// <returns>Collection of <see cref="IModuleInfo"/> dependants of <paramref name="moduleInfo"/>.</returns>
protected virtual IEnumerable<IModuleInfo> GetDependentModulesInner(IModuleInfo moduleInfo)
{
return Modules.Where(dependantModule => moduleInfo.DependsOn.Contains(dependantModule.ModuleName));
}
/// <summary>
/// Ensures that the catalog is validated.
/// </summary>View on GitHub (pinned to 358118cd64)