{"record":{"id":"2b68139288561987","repo":"dotnet/wpf","slug":"message-invalidoperationexception","errorCode":null,"errorMessage":"message (InvalidOperationException)","messagePattern":"message \\(InvalidOperationException\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlParseException.cs","lineNumber":467,"sourceCode":"\n#endif\n\n            // If the exception was a XamlParse exception on the other\n            // side of a Reflection Invoke, then just pull up the Parse exception.\n            if (innerException is TargetInvocationException && innerException.InnerException is XamlParseException)\n            {\n                xamlParseException = (XamlParseException)innerException.InnerException;\n            }\n            else\n            {\n                if (lineNumber > 0)\n                {\n                    xamlParseException = new XamlParseException(message, lineNumber, linePosition, innerException);\n                }\n                else\n                {\n#if PBTCOMPILER\n                    throw new InvalidOperationException(message);\n#else\n                    xamlParseException = new XamlParseException(message, innerException);\n#endif\n                }\n            }\n\n            // Fill in the exception with some more runtime-context information.\n            if (contextXamlObjectIds != null)\n            {\n                xamlParseException.NameContext = contextXamlObjectIds.Name;\n                xamlParseException.UidContext = contextXamlObjectIds.Uid;\n                xamlParseException.KeyContext = contextXamlObjectIds.Key;\n            }\n            xamlParseException.BaseUri = baseUri;\n\n\n#if !PBTCOMPILER\n            if (TraceMarkup.IsEnabled)","sourceCodeStart":449,"sourceCodeEnd":485,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlParseException.cs#L449-L485","documentation":"XamlParseException's helper (around line 467) wraps a parse failure. When line/column info is unavailable it constructs a plain XamlParseException(message, innerException); under the PBTCOMPILER build (markup compiler) it instead throws InvalidOperationException(message) because XamlParseException is not the right surface at compile time.","triggerScenarios":"XAML parsing error reported without line information while running inside the markup compiler (PBTCOMPILER) build, where the helper falls through to the #if PBTCOMPILER branch.","commonSituations":"Compiling XAML with markup compiler (presenting/deferred content), malformed XAML in .xaml files, missing assemblies referenced by XAML during build.","solutions":["Fix the underlying XAML error described by the message (bad element, missing namespace, type not found).","Check that all assemblies referenced by xmlns mappings are available to the compiler.","When catching at build time, handle InvalidOperationException from the markup compiler rather than XamlParseException."],"exampleFix":"// before\ncatch (XamlParseException ex) { Handle(ex); }\n// after\n#if PBTCOMPILER\ncatch (InvalidOperationException ex) { Handle(ex.Message); }\n#else\ncatch (XamlParseException ex) { Handle(ex); }\n#endif","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"#if PBTCOMPILER\ntry { ParseXaml(file); }\ncatch (InvalidOperationException ex) { ReportBuildError(ex.Message, file); }\n#else\ntry { ParseXaml(file); }\ncatch (XamlParseException ex) { ReportError(ex.Message, ex.LineNumber, ex.LinePosition); }\n#endif","preventionTips":["Validate XAML files and xmlns mappings before compilation.","Handle the markup compiler's InvalidOperationException when running under PBTCOMPILER builds."],"tags":["wpf","xaml","parse-error","markup-compiler"],"backgroundTag":"xml-parse-error","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"}