dotnet/wpf · error · InvalidOperationException
SR.BamlWriterClosed
Error message
SR.BamlWriterClosed
What it means
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.
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
Example fix
// before var w = new BamlWriter(s); w.Serialize(obj); w.Close(); w.Serialize(obj2); // after using (var w1 = new BamlWriter(s1)) w1.Serialize(obj); using (var w2 = new BamlWriter(s2)) w2.Serialize(obj2);
Defensive patterns
Strategy: type-guard
Validate before calling
// guard before use
if (writerIsClosed) throw new InvalidOperationException("Create a new BamlWriter; the current one is closed"); Type guard
static bool CanStartDocument(BamlWriter w) => !wIsClosed(w); // expose or track _closed in your wrapper
Try / catch
try { writer.WriteStartDocument(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("closed") || ex.Message.Contains("document")) {
writer = new BamlWriter(stream); // recreate and retry once
} Prevention
- Treat BamlWriter as single-use
- Use 'using' scoped to one serialization
- Never call Serialize after Close
- Create a fresh writer per object/document
When it happens
Trigger: Calling WriteStartDocument() after Close()/Dispose, or (via Serialize) reusing a BamlWriter for a second document — _closed flag set from prior Close().
Common situations: Serializing multiple objects by reusing one BamlWriter, calling Serialize after explicitly closing, event-driven code that closes the stream and then tries another write.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- SR.BamlReaderClosed
- SR.ParserMultiBamls
- Can't Assign to Known Type attributes
- Could not find prefix for type
- Current DocumentSequence, FixedDocument, or FixedPage not…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ce67b386085d44c1.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs:145
}
#endregion IParserHelper
#region Record Writing
/// <summary>
/// Write the start of document record, giving the baml version string
/// </summary>
/// <remarks>
/// This must be the first call made when creating a new baml file. This
/// is needed to specify the start of the document and baml version.
/// </remarks>
public void WriteStartDocument()
{
if (_closed)
{
throw new InvalidOperationException(SR.BamlWriterClosed);
}
if (_startDocumentWritten)
{
throw new InvalidOperationException(SR.BamlWriterStartDoc);
}
XamlDocumentStartNode node = new XamlDocumentStartNode(0,0,_depth);
_bamlRecordWriter.WriteDocumentStart(node);
_startDocumentWritten = true;
Push(BamlRecordType.DocumentStart);
}
/// <summary>
/// Write the end of document record.
/// </summary>
/// <remarks>
/// This must be the last call made when creating a new baml file.
/// </remarks>View on GitHub (pinned to 81131a70a4)