dotnet/wpf · error · InvalidOperationException

SR.InvalidDeSerialize

Error message

SR.InvalidDeSerialize

What it means

XamlSerializer.ConvertXamlToBaml is a virtual method whose base implementation always throws InvalidOperationException(SR.InvalidDeSerialize). It is called (e.g. from WriteElementStart) when a serializer that does not support XAML-to-BAML conversion is asked to convert a node, meaning the concrete XamlSerializer subclass never overrode this method.

Solutions

  1. Override ConvertXamlToBaml in your XamlSerializer subclass to perform the conversion (or delegate to the appropriate base).
  2. Use a serializer class that supports the XAML-to-BAML direction (e.g. the appropriate ValueSerializer type for the value).
  3. Check which serializer was registered for the type and ensure it matches the pipeline direction being executed.

Example fix

// before
class MySerializer : XamlSerializer { }
// after
class MySerializer : XamlSerializer {
    internal override void ConvertXamlToBaml(XamlReaderHelper tokenReader, ParserContext context, XamlNode node, BamlRecordWriter writer) {
        // implement conversion or call base helper
    }
}
Defensive patterns

Strategy: type-guard

Validate before calling

var m = serializer.GetType().GetMethod("ConvertXamlToBaml", BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);
bool canConvert = m != null && m.DeclaringType != typeof(XamlSerializer);

Type guard

static bool SupportsXamlToBaml(XamlSerializer s) => s.GetType().GetMethod("ConvertXamlToBaml", BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public)!.DeclaringType != typeof(XamlSerializer);

Try / catch

try { serializer.ConvertXamlToBaml(reader, ctx, node, writer); } catch (InvalidOperationException ex) when (ex.Message.Contains("serialize") || ex.Message.Contains("DeSerialize")) { log.Error($"{serializer.GetType().Name} does not support XAML->BAML"); throw new NotSupportedException(ex.Message, ex); }

Prevention

When it happens

Trigger: Invoking a custom/derived XamlSerializer's ConvertXamlToBaml (via BamlRecordWriter / WriteElementStart) on a serializer type that only supports the live-object path and did not override ConvertXamlToBaml.

Common situations: Using XamlWriter.Save/BAML compilation pipelines with a serializer subclass lacking the conversion override; applying an optimized serializer (designed only for object<->binary) to the XAML->BAML path.

Understand the failure class

Background: "NotImplementedError: Subclasses should override this method" / "must be implemented" — abstract method errors explained — this error's family across 40 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlSerializer.cs:58

        }
        
        #endregion Construction

        #region OtherConversions
        
        /// <summary>
        ///   Convert from Xaml read by a token reader into baml being written
        ///   out by a record writer.  The context gives mapping information.
        /// </summary>
#if !PBTCOMPILER
#endif        
        internal virtual void ConvertXamlToBaml (
            XamlReaderHelper          tokenReader,
            ParserContext       context,
            XamlNode            xamlNode,
            BamlRecordWriter    bamlWriter)
        {
            throw new InvalidOperationException(SR.InvalidDeSerialize);
        }

#if !PBTCOMPILER

        /// <summary>
        ///   Convert from Xaml read by a token reader into a live
        ///   object tree.  The context gives mapping information.
        /// </summary>
        internal virtual void ConvertXamlToObject (
            XamlReaderHelper             tokenReader,
            ReadWriteStreamManager streamManager,
            ParserContext          context,
            XamlNode               xamlNode,
            BamlRecordReader       reader)
        {
            throw new InvalidOperationException(SR.InvalidDeSerialize);
        }

View on GitHub (pinned to 81131a70a4)