dotnet/wpf · error · XpsSerializationException
SR.XpsSerializerFactory_WriterIsClosed
Error message
SR.XpsSerializerFactory_WriterIsClosed
What it means
XpsSerializerWriterCollator.CheckDisposed throws XpsSerializationException(SR.XpsSerializerFactory_WriterIsClosed) when the underlying collator (_collator) has been released. It is called by Write, WriteAsync, Close, CancelAsync, and Cancel, so any use of a closed collator fails with this error.
Solutions
- Stop using the collator after Close; obtain a fresh collator from the serializer for new content.
- Ensure Close/Cancel is called only once and track the closed state.
- Guard collation code paths with a disposed flag.
Example fix
// before collator.Close(); collator.Write(canvas); // throws // after collator.Close(); collator = serializer.CreateVisualsCollator(); collator.Write(canvas);
Defensive patterns
Strategy: type-guard
Validate before calling
if (collator == null || collator.IsClosed) throw new InvalidOperationException("collator already closed"); Type guard
bool CollatorUsable(XpsSerializerWriterCollator c) => c != null && !c.IsDisposed;
Try / catch
try { collator.Write(visual); }
catch (XpsSerializationException) when (closed) { /* recreate collator */ } Prevention
- Call Close/Cancel exactly once and guard with a bool flag.
- Don't write pages after Close; create a new collator for new content.
- Keep collator lifetime scoped to the document being produced.
When it happens
Trigger: Calling Write, WriteAsync, Close, CancelAsync, or Cancel on an XpsSerializerWriterCollator after it was closed and its internal _collator set to null.
Common situations: Writing additional pages after Close; double-Close; Cancel/CancelAsync invoked after the collator finished and released its resources.
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/b8fc2f20e79c029d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/SerializerFactory/XpsSerializerWriterCollator.cs:119
/// <summary>
/// Cancel Write
/// </summary>
public override void Cancel()
{
CheckDisposed();
_collator.Cancel();
}
#endregion
#region Private Methods
private void CheckDisposed()
{
if (_collator == null)
{
throw new XpsSerializationException(SR.XpsSerializerFactory_WriterIsClosed);
}
}
#endregion
#region Data
private VisualsToXpsDocument _collator;
private Package _package;
private XpsDocument _xpsDocument;
#endregion
}
}
View on GitHub (pinned to 81131a70a4)