dotnet/wpf · error · InvalidOperationException

message (InvalidOperationException)

Error message

message (InvalidOperationException)

What it means

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.

Solutions

  1. Fix the underlying XAML error described by the message (bad element, missing namespace, type not found).
  2. Check that all assemblies referenced by xmlns mappings are available to the compiler.
  3. When catching at build time, handle InvalidOperationException from the markup compiler rather than XamlParseException.

Example fix

// before
catch (XamlParseException ex) { Handle(ex); }
// after
#if PBTCOMPILER
catch (InvalidOperationException ex) { Handle(ex.Message); }
#else
catch (XamlParseException ex) { Handle(ex); }
#endif
Defensive patterns

Strategy: try-catch

Try / catch

#if PBTCOMPILER
try { ParseXaml(file); }
catch (InvalidOperationException ex) { ReportBuildError(ex.Message, file); }
#else
try { ParseXaml(file); }
catch (XamlParseException ex) { ReportError(ex.Message, ex.LineNumber, ex.LinePosition); }
#endif

Prevention

When it happens

Trigger: 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.

Common situations: Compiling XAML with markup compiler (presenting/deferred content), malformed XAML in .xaml files, missing assemblies referenced by XAML during build.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/2b68139288561987. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlParseException.cs:467

#endif

            // If the exception was a XamlParse exception on the other
            // side of a Reflection Invoke, then just pull up the Parse exception.
            if (innerException is TargetInvocationException && innerException.InnerException is XamlParseException)
            {
                xamlParseException = (XamlParseException)innerException.InnerException;
            }
            else
            {
                if (lineNumber > 0)
                {
                    xamlParseException = new XamlParseException(message, lineNumber, linePosition, innerException);
                }
                else
                {
#if PBTCOMPILER
                    throw new InvalidOperationException(message);
#else
                    xamlParseException = new XamlParseException(message, innerException);
#endif
                }
            }

            // Fill in the exception with some more runtime-context information.
            if (contextXamlObjectIds != null)
            {
                xamlParseException.NameContext = contextXamlObjectIds.Name;
                xamlParseException.UidContext = contextXamlObjectIds.Uid;
                xamlParseException.KeyContext = contextXamlObjectIds.Key;
            }
            xamlParseException.BaseUri = baseUri;


#if !PBTCOMPILER
            if (TraceMarkup.IsEnabled)

View on GitHub (pinned to 81131a70a4)