{"record":{"id":"9033b919075c3600","repo":"dotnet/wpf","slug":"sr-bamlwriterbadscope-0-1","errorCode":null,"errorMessage":"SR.BamlWriterBadScope: {0} {1}","messagePattern":"SR\\.BamlWriterBadScope: (.+?) (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs","lineNumber":794,"sourceCode":"                    XamlPropertyIDictionaryEndNode dictionaryEnd = \n                                      new XamlPropertyIDictionaryEndNode(\n                                                  0, \n                                                  0, \n                                                  --_depth);\n                    _bamlRecordWriter.WritePropertyIDictionaryEnd(dictionaryEnd);\n                    break;\n                    \n                case BamlRecordType.PropertyComplexStart:\n                    XamlPropertyComplexEndNode complexEnd = \n                                       new XamlPropertyComplexEndNode(\n                                                  0, \n                                                  0, \n                                                  --_depth);\n                    _bamlRecordWriter.WritePropertyComplexEnd(complexEnd);\n                    break;\n\n                default:\n                    throw new InvalidOperationException(\n                                    SR.Format(SR.BamlWriterBadScope,\n                                           startTagType.ToString(),\n                                           BamlRecordType.PropertyComplexEnd.ToString()));\n            }                        \n            _parserContext.PopScope();\n        }\n\n        /// <summary>\n        /// Write a literal content record to baml stream\n        /// </summary>\n        public void WriteLiteralContent(\n            string contents)\n        {\n            VerifyWriteState();\n            ProcessMarkupExtensionNodes();\n\n            XamlLiteralContentNode literalContent = new XamlLiteralContentNode(\n                                                               0,","sourceCodeStart":776,"sourceCodeEnd":812,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs#L776-L812","documentation":"WriteEndComplexProperty inspects the start-tag type of the currently open complex property and writes the matching end record. If the start tag is not a PropertyComplexStart (default case in its switch), it throws InvalidOperationException(SR.BamlWriterBadScope, startTagType, PropertyComplexEnd), indicating the end call does not match the open scope. This guards against mismatched push/pop of BAML records.","triggerScenarios":"Calling WriteEndComplexProperty when the open scope was actually started by an array start, IList start, IDictionary start, or element start — i.e. the wrong End method for the scope.","commonSituations":"Using WriteEndComplexProperty to close WriteStartArrayProperty/WriteStartIListProperty/WriteStartIDictionaryProperty scopes in a custom serializer; copy-pasted end calls in a state machine; exception unwinding that skipped the matching End call.","solutions":["Call the matching End method: WriteEndArrayProperty, WriteEndIListProperty, or WriteEndIDictionaryProperty for their respective Start calls.","Keep a stack of started scope types and pop with the exact matching End call.","Fix ordering so element starts are closed with WriteEndElement, not WriteEndComplexProperty."],"exampleFix":"// before\nwriter.WriteStartIListProperty(\"Children\");\nwriter.WriteEndComplexProperty(); // mismatch\n// after\nwriter.WriteStartIListProperty(\"Children\");\nwriter.WriteEndIListProperty();","handlingStrategy":"validation","validationCode":"var scopeStack = new Stack<BamlRecordType>();\nvoid EndScope(BamlWriter w)\n{\n    var start = scopeStack.Peek();\n    if (start == BamlRecordType.PropertyComplexStart) w.WriteEndComplexProperty();\n    else if (start == BamlRecordType.PropertyArrayStart) w.WriteEndArrayProperty();\n    else if (start == BamlRecordType.PropertyIListStart) w.WriteEndIListProperty();\n    else if (start == BamlRecordType.PropertyIDictionaryStart) w.WriteEndIDictionaryProperty();\n    scopeStack.Pop();\n}","typeGuard":null,"tryCatchPattern":"try { writer.WriteEndComplexProperty(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"BamlWriterBadScope\") || ex.Message.Contains(\"Property\"))\n{\n    // unwind scopes in a finally/stack-based manner instead\n}","preventionTips":["Always pair Start/End calls via a scope stack.","Never use WriteEndComplexProperty for array/IList/dictionary scopes.","Close scopes in reverse order of opening."],"tags":["wpf","baml","xaml-serialization","scope-mismatch","invalid-operation"],"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"}