dotnet/wpf · error · XpsSerializationException
SR.ReachSerialization_CannotReleaseXmlWriter
Error message
SR.ReachSerialization_CannotReleaseXmlWriter
What it means
XpsSerializationException thrown by NullPackagingPolicy.ReleaseXmlWriterForFixedDocumentSequence when internal state does not match an active fixed-document-sequence writer. The policy tracks current writer reference counts/IDs; releasing a writer outside a valid begin/release sequence leaves no matching writer to release, indicating a lifecycle invariant violation.
Solutions
- Ensure every ReleaseXmlWriterForFixedDocumentSequence call is paired with a prior acquire/start of a fixed-document-sequence writer, with no double release.
- Verify release ordering: sequence writers are released only after their contained documents/pages are finished.
- Wrap the serialization pipeline so exceptions do not cause a release to run without its matching acquire.
- Check any custom NullPackagingPolicy/BasePackagingPolicy overrides for unbalanced writer reference bookkeeping (_currentFixedDocumentSequenceWriterRef).
Example fix
// before
policy.ReleaseXmlWriterForFixedDocumentSequence(); // no sequence writer started
// after
if (sequenceWriterAcquired)
{
policy.ReleaseXmlWriterForFixedDocumentSequence();
sequenceWriterAcquired = false;
} Defensive patterns
Strategy: try-catch
Validate before calling
if (!fixedDocumentSequenceWriterActive)
throw new InvalidOperationException("Cannot release FixedDocumentSequence writer: none acquired"); Type guard
bool CanReleaseSequenceWriter(NullPackagingPolicy p) => p.CurrentFixedDocumentSequenceWriter != null;
Try / catch
try { policy.ReleaseXmlWriterForFixedDocumentSequence(); }
catch (XpsSerializationException ex) when (ex.Message.Contains("CannotReleaseXmlWriter"))
{ /* mark lifecycle state invalid and abort/document the run */ } Prevention
- Maintain a boolean/counter in your own code mirroring each acquire/release pair.
- Release writers in strict nesting order (page, document, sequence).
- Ensure exception handlers do not release writers twice.
- Prefer high-level XpsDocument APIs over manual packaging-policy calls.
When it happens
Trigger: Calling ReleaseXmlWriterForFixedDocumentSequence (or the packaging policy API that drives it) when no FixedDocumentSequence writer was previously acquired/started, or after it was already released.
Common situations: Unbalanced acquire/release calls in custom XPS packaging code; exception paths that skip a Release or double-Release; interleaving document/page/sequence releases out of order in custom PackagingPolicy implementations.
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
- Cannot find the appropriate serializer.
- Cannot find the appropriate serializer.
- Cannot find the appropriate serializer.
- Cannot instantiate a serializer of the given type.
- CurrentFixedPageWriter uninitialized
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9dc35377c8cb8b14.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NullPackagingPolicy.cs:118
{
_currentDocumentSequenceWriterRef--;
if(_currentDocumentSequenceWriterRef == 0)
{
//
// if any of the other low level writer exist, then
// throw an exception or is it better if we just close
// them and consider that if any additional call on them
// would be the one that throws the expcetion
//
//_currentFixedDocumentSequenceWriter.Commit();
Initialize();
InitializeResourceReferences();
}
}
else
{
throw new XpsSerializationException(SR.ReachSerialization_CannotReleaseXmlWriter);
}
}
/// <Summary>
///
/// </Summary>
public
override
XmlWriter
AcquireXmlWriterForFixedDocument(
)
{
XmlWriter xmlWriter = null;
if(_currentFixedDocumentWriterRef == 0)
{
//
// We need to create the corresponding part in the Xps packageView on GitHub (pinned to 81131a70a4)