PrismLibrary/Prism · error · CyclicDependencyFoundException

Resources.CyclicDependencyFound

Error message

Resources.CyclicDependencyFound

What it means

CyclicDependencyFoundException is thrown by ModuleDependencySolver.Solve when the module dependency graph contains a cycle, so no topological load order exists. Prism requires an acyclic graph to order module initialization.

Solutions

  1. Break the cycle by removing or reversing one dependency edge
  2. Extract the shared functionality into a new lower-level module that both depend on
  3. Use TrySolve, which returns false instead of throwing, to detect cycles gracefully

Example fix

// before
solver.AddDependency("ModuleA", "ModuleB");
solver.AddDependency("ModuleB", "ModuleA"); // cycle
// after
solver.AddDependency("ModuleA", "ModuleB"); // one direction only
Defensive patterns

Strategy: validation

Validate before calling

bool HasCycle(Dictionary<string,string> edges)
{
    var visited = new HashSet<string>();
    foreach (var n in edges.Keys)
    {
        var cur = n;
        while (cur != null && edges.ContainsKey(cur))
        {
            if (visited.Contains(cur)) return true;
            visited.Add(cur);
            cur = edges[cur];
        }
        visited.Clear();
    }
    return false;
}

Try / catch

try
{
    var order = solver.Solve();
}
catch (CyclicDependencyFoundException)
{
    logger.LogError("Module dependency graph contains a cycle");
}

Prevention

When it happens

Trigger: ModuleA depends on ModuleB while ModuleB depends on ModuleA (directly or via longer chains); wiring mutual dependencies in AddDependency; config files where each module lists the other as a dependency.

Common situations: Refactoring that accidentally reversed a dependency direction; shared-service module mistakenly depending on a consumer; copying dependency attributes between module declarations.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/193f03ab28ea9cd6. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Modularity/ModuleDependencySolver.cs:84

            }
        }

        /// <summary>
        /// Calculates an ordered vector according to the defined dependencies.
        /// Non-dependant modules appears at the beginning of the resulting array.
        /// </summary>
        /// <returns>The resulting ordered list of modules.</returns>
        /// <exception cref="CyclicDependencyFoundException">This exception is thrown
        /// when a cycle is found in the defined dependency graph.</exception>
        public string[] Solve()
        {
            List<string> skip = new List<string>();
            while (skip.Count < dependencyMatrix.Count)
            {
                List<string> leaves = this.FindLeaves(skip);
                if (leaves.Count == 0 && skip.Count < dependencyMatrix.Count)
                {
                    throw new CyclicDependencyFoundException(Resources.CyclicDependencyFound);
                }
                skip.AddRange(leaves);
            }
            skip.Reverse();

            if (skip.Count > knownModules.Count)
            {
                string moduleNames = this.FindMissingModules(skip);
                throw new ModularityException(moduleNames, string.Format(CultureInfo.CurrentCulture,
                                                            Resources.DependencyOnMissingModule,
                                                            moduleNames));
            }

            return skip.ToArray();
        }

        private string FindMissingModules(List<string> skip)
        {

View on GitHub (pinned to 358118cd64)