dotnet/wpf · error · XpsSerializationException
SR.XpsSerializerFactory_WriterIsClosed
Error message
SR.XpsSerializerFactory_WriterIsClosed
What it means
XpsSerializerWriter.CheckDisposed throws XpsSerializationException(SR.XpsSerializerFactory_WriterIsClosed) when an operation is attempted after the writer has been closed/finalized (its _xpsDocumentWriter field is nulled by FinalizeWriter). It guards Write, WriteAsync, CancelAsync, and CreateVisualsCollator — using a closed serializer writer is invalid state.
Solutions
- Do not use the writer after Close/Dispose; create a new XpsSerializerWriter for further writes.
- Track writer lifetime and null out references once closed.
- Guard calls with an IsDisposed/IsClosed check before invoking write APIs.
Example fix
// before writer.Close(); writer.Write(page); // throws // after writer.Close(); writer = serializerFactory.CreateXpsSerializerWriter(package); writer.Write(page);
Defensive patterns
Strategy: type-guard
Validate before calling
if (writer == null || writer.IsClosed) throw new InvalidOperationException("writer already closed"); Type guard
bool CanWrite(XpsSerializerWriter w) => w != null && !w.IsDisposed;
Try / catch
try { writer.Write(page); }
catch (XpsSerializationException ex) when (writerClosed) { /* recreate writer */ } Prevention
- Treat Close/Dispose as terminal; never reuse the writer afterwards.
- Null out writer references once closed to make misuse obvious.
- Serialize write calls; don't call CancelAsync after Close.
When it happens
Trigger: Calling Write, WriteAsync, CancelAsync, or CreateVisualsCollator on an XpsSerializerWriter after Close/dispose (FinalizeWriter already ran and set _xpsDocumentWriter to null).
Common situations: Writing additional documents after closing the XPS package; reusing a cached writer that was disposed; CancelAsync racing with Close.
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
- FixedPageReader
- SR.XpsSerializerFactory_WriterIsClosed
- " }} " element found. Expected fixed page element ( }} ).
- ' ' ContentType is not valid.
- ' ' ContentType is not valid.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/7f8620ab00929381.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/SerializerFactory/XpsSerializerWriter.cs:473
FinalizeWriter();
WritingCompleted.Invoke(sender, e);
}
}
private void xsw_WritingCancelled(object sender, WritingCancelledEventArgs e)
{
if ( WritingCancelled != null)
{
FinalizeWriter();
WritingCancelled.Invoke(sender, e);
}
}
private void CheckDisposed()
{
if (_xpsDocumentWriter == null)
{
throw new XpsSerializationException(SR.XpsSerializerFactory_WriterIsClosed);
}
}
private void FinalizeWriter()
{
_xpsDocument.Close();
_package.Close();
_xpsDocument = null;
_xpsDocumentWriter = null;
_package = null;
}
#endregion
#region Data
private Package _package;
private XpsDocument _xpsDocument;View on GitHub (pinned to 81131a70a4)