{"record":{"id":"34ed5bbd19409dae","repo":"dotnet/wpf","slug":"exceptionmessage-tostring","errorCode":null,"errorMessage":"exceptionMessage.ToString()","messagePattern":"exceptionMessage\\.ToString\\(\\)","errorType":"exception","errorClass":"XamlObjectWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/NameFixupGraph.cs","lineNumber":515,"sourceCode":"                string meName = token.ReferencedObject.ToString();\n                if (token.LineNumber != 0)\n                {\n                    if (token.LinePosition != 0)\n                    {\n                        exceptionMessage.Append(SR.Format(SR.LineNumberAndPosition, meName, token.LineNumber, token.LinePosition));\n                    }\n                    else\n                    {\n                        exceptionMessage.Append(SR.Format(SR.LineNumberOnly, meName, token.LineNumber));\n                    }\n                }\n                else\n                {\n                    exceptionMessage.Append(meName);\n                }\n            }\n\n            throw new XamlObjectWriterException(exceptionMessage.ToString());\n        }\n    }\n}\n","sourceCodeStart":497,"sourceCodeEnd":519,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/NameFixupGraph.cs#L497-L519","documentation":"NameFixupGraph.ThrowProvideValueCycle throws XamlObjectWriterException when the XAML object graph contains a cycle in ProvideValue/deferred-content dependencies that prevents name fixup from completing. The exception message is built by appending each remaining object's dependency name until the cycle is described.","triggerScenarios":"Loading XAML (XamlObjectWriter / Baml) whose object graph has circular references through markup extensions or deferred ProvideValue sites, e.g. resource dictionaries or templates referencing each other so name fixup never terminates.","commonSituations":"Cyclic StaticResource/Template references between resources; two XAML objects whose markup extensions reference one another; complex deferred content in App.xaml or merged dictionaries.","solutions":["Break the cycle in the XAML: restructure resources/templates so no object references (directly or via ProvideValue) back to itself or its ancestors.","Replace one side of the cycle with a code-behind assignment done after load completes.","Read the exception message's remaining-dependency list to identify the exact objects forming the cycle and remove one link.","Convert one deferred reference (e.g. DynamicResource cycle) into an eagerly-resolved value."],"exampleFix":"<!-- before: A references B, B references A via markup extension cycles -->\n<Style x:Key=\"A\" BasedOn=\"{StaticResource B}\" ... />\n<Style x:Key=\"B\" BasedOn=\"{StaticResource A}\" ... />\n<!-- after: break the cycle with a common base -->\n<Style x:Key=\"Base\" TargetType=\"Button\" />\n<Style x:Key=\"A\" BasedOn=\"{StaticResource Base}\" ... />\n<Style x:Key=\"B\" BasedOn=\"{StaticResource Base}\" ... />","handlingStrategy":"validation","validationCode":"// Static sanity check: ensure no resource key is referenced (directly or transitively) by a definition with the same key\nforeach (var key in resourceKeys)\n    if (DependsOn(resourceDict[key], key)) throw new InvalidOperationException($\"Cycle detected at resource '{key}'\");","typeGuard":null,"tryCatchPattern":"try { LoadXaml(xamlReader); }\ncatch (XamlObjectWriterException ex) when (ex.Message.Contains(\"cycle\") || ex.Message.Contains(\"ProvideValue\"))\n{ /* surface the listed dependencies, ask user to break the cycle */ }","preventionTips":["Keep resource dictionaries acyclic: base styles should not reference derived keys.","Prefer a shared base resource over mutual BasedOn references.","Load XAML in tests during CI to catch cycles before runtime.","Resolve cyclic dependencies in code-behind after load when dynamic linkage is required."],"tags":["wpf","xaml","cycle","object-graph","providevalue"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}