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

  1. Change the OnDemand dependency module to WhenAvailable so it initializes before its dependents.
  2. Alternatively change the dependent startup module to OnDemand so it loads later with its dependency.
  3. 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

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


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)