{"record":{"id":"e38869483a7723af","repo":"dotnet/wpf","slug":"sr-bamlwriterunknownmarkupextension","errorCode":null,"errorMessage":"SR.BamlWriterUnknownMarkupExtension","messagePattern":"SR\\.BamlWriterUnknownMarkupExtension","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs","lineNumber":997,"sourceCode":"                    _bamlRecordWriter.WritePropertyComplexStart((XamlPropertyComplexStartNode)node);\n                    break;\n                case XamlNodeType.PropertyComplexEnd:\n                    _bamlRecordWriter.WritePropertyComplexEnd((XamlPropertyComplexEndNode)node);\n                    break;\n                case XamlNodeType.Text:\n                    _bamlRecordWriter.WriteText((XamlTextNode)node);\n                    break;\n                case XamlNodeType.EndAttributes:\n                    _bamlRecordWriter.WriteEndAttributes((XamlEndAttributesNode)node);\n                    break;\n                case XamlNodeType.ConstructorParametersStart:\n                    _bamlRecordWriter.WriteConstructorParametersStart((XamlConstructorParametersStartNode)node);\n                    break;\n                case XamlNodeType.ConstructorParametersEnd:\n                    _bamlRecordWriter.WriteConstructorParametersEnd((XamlConstructorParametersEndNode)node);\n                    break;\n                default:\n                    throw new InvalidOperationException(SR.BamlWriterUnknownMarkupExtension);\n            }\n        }\n        _markupExtensionNodes.Clear();\n    }\n\n    /***************************************************************************\\\n    *\n    * BamlWriter.VerifyWriteState\n    *\n    * Verify that we are in a good state to perform a record write.  Throw\n    * appropriate exceptions if not.\n    *\n    \\***************************************************************************/\n    \n    private void VerifyWriteState()\n    {\n        if (_closed)\n        {","sourceCodeStart":979,"sourceCodeEnd":1015,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs#L979-L1015","documentation":"During markup-extension expansion, ProcessMarkupExtensionNodes replays buffered XAML nodes and writes them as BAML records; its switch has no case for the node type it received, so it throws InvalidOperationException(SR.BamlWriterUnknownMarkupExtension). It means the writer encountered an unexpected XamlNodeType while expanding a markup extension — an internal protocol/state violation rather than bad user data per se.","triggerScenarios":"A markup extension (e.g. StaticResource, Binding, TemplateBinding) expansion produces a node type outside the handled set while being flushed by WriteStartElement, WriteEndElement, WriteStartComplexProperty, WriteLiteralContent, WritePIMapping, or WriteText; feeding nodes the writer's state machine did not anticipate.","commonSituations":"Custom or exotic markup extensions in serialized XAML; node-stream order anomalies from a preceding XamlReader stage; WPF framework bugs in rarely used extension constructs.","solutions":["Simplify the markup extension in the source XAML (e.g. replace complex nested extensions with resource references or plain values).","Update to a patched WPF version if the node type is legitimately supported upstream.","Pre-expand markup extensions to literal values (via IXamlSchemaContextProvider/MarkupExtension resolution) before BAML writing."],"exampleFix":"// before\n<Button Background=\"{StaticResource {x:Static SystemColors.ControlBrushKey}}\" />\n// after\n<Button Background=\"{StaticResource ControlBrush}\" /> <!-- simpler, pre-resolvable extension -->","handlingStrategy":"try-catch","validationCode":"// Reject exotic/unrecognized markup extension nodes before feeding the writer:\nvar known = new[] { typeof(StaticResourceExtension), typeof(DynamicResourceExtension), typeof(Binding), typeof(TemplateBindingExtension) };\nbool isKnownExtension(object ext) => known.Contains(ext.GetType());","typeGuard":"bool IsSupportedExtensionType(object me) =>\n    me is StaticResourceExtension || me is DynamicResourceExtension ||\n    me is Binding || me is TemplateBindingExtension;","tryCatchPattern":"try { /* serialization step that flushes markup extension nodes */ }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"markup extension\"))\n{\n    // retry after pre-expanding the offending extension to a literal value\n}","preventionTips":["Pre-resolve markup extensions to concrete values before BAML writing.","Avoid exotic or nested markup extensions in serialized XAML.","Keep WPF assemblies patched; some extension node handling bugs are fixed in servicing releases."],"tags":["wpf","baml","markup-extension","invalid-operation"],"backgroundTag":"internal-invariant-violation","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"}