{"record":{"id":"ce67b386085d44c1","repo":"dotnet/wpf","slug":"sr-bamlwriterclosed","errorCode":null,"errorMessage":"SR.BamlWriterClosed","messagePattern":"SR\\.BamlWriterClosed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs","lineNumber":145,"sourceCode":"        }\n        \n\n#endregion IParserHelper\n\n#region Record Writing\n\n        /// <summary>\n        /// Write the start of document record, giving the baml version string\n        /// </summary>\n        /// <remarks>\n        /// This must be the first call made when creating a new baml file.  This\n        /// is needed to specify the start of the document and baml version.\n        /// </remarks>\n        public void WriteStartDocument()\n        {\n            if (_closed)\n            {\n                throw new InvalidOperationException(SR.BamlWriterClosed);\n            }\n            if (_startDocumentWritten)\n            {\n                throw new InvalidOperationException(SR.BamlWriterStartDoc);\n            }\n            \n            XamlDocumentStartNode node = new XamlDocumentStartNode(0,0,_depth);\n            _bamlRecordWriter.WriteDocumentStart(node);\n            _startDocumentWritten = true;\n            Push(BamlRecordType.DocumentStart);\n        }\n\n        /// <summary>\n        /// Write the end of document record.\n        /// </summary>\n        /// <remarks>\n        /// This must be the last call made when creating a new baml file.\n        /// </remarks>","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs#L127-L163","documentation":"BamlWriter.WriteStartDocument throws InvalidOperationException(SR.BamlWriterClosed) if the writer was already Closed, and SR.BamlWriterStartDoc if WriteStartDocument was already called. Writers are single-use: once closed, no further document writing is permitted.","triggerScenarios":"Calling WriteStartDocument() after Close()/Dispose, or (via Serialize) reusing a BamlWriter for a second document — _closed flag set from prior Close().","commonSituations":"Serializing multiple objects by reusing one BamlWriter, calling Serialize after explicitly closing, event-driven code that closes the stream and then tries another write.","solutions":["Create a new BamlWriter for each document; writers are single-shot","Check _closed/IsClosed before calling WriteStartDocument (guard in caller code)","Do not call Close before Serialize completes; use 'using' scoped tightly around a single serialization","If serializing many objects, wrap creation in a helper that yields a fresh writer per call"],"exampleFix":"// before\nvar w = new BamlWriter(s); w.Serialize(obj); w.Close(); w.Serialize(obj2);\n// after\nusing (var w1 = new BamlWriter(s1)) w1.Serialize(obj);\nusing (var w2 = new BamlWriter(s2)) w2.Serialize(obj2);","handlingStrategy":"type-guard","validationCode":"// guard before use\nif (writerIsClosed) throw new InvalidOperationException(\"Create a new BamlWriter; the current one is closed\");","typeGuard":"static bool CanStartDocument(BamlWriter w) => !wIsClosed(w); // expose or track _closed in your wrapper","tryCatchPattern":"try { writer.WriteStartDocument(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"closed\") || ex.Message.Contains(\"document\")) {\n    writer = new BamlWriter(stream); // recreate and retry once\n}","preventionTips":["Treat BamlWriter as single-use","Use 'using' scoped to one serialization","Never call Serialize after Close","Create a fresh writer per object/document"],"tags":["wpf","baml","writer","invalid-state"],"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"}