dotnet/wpf · error · InvalidOperationException

CurrentFixedPageWriter uninitialized

Error message

CurrentFixedPageWriter uninitialized

What it means

AcquireXmlWriterForPage returns the page-content XmlWriter, but requires the current fixed-page writer to have been initialized first via AcquireXmlWriterForFixedPage. If _currentFixedPageXmlWriter is null (no page is open), InvalidOperationException("CurrentFixedPageWriter uninitialized") is thrown.

Solutions

  1. Call AcquireXmlWriterForFixedPage before AcquireXmlWriterForPage.
  2. Check the page writer is still open (page not yet released) before acquiring page-content writers.
  3. Follow the standard lifecycle: acquire page -> acquire page content/resources -> write -> release all -> release page.

Example fix

// before
XmlWriter w = policy.AcquireXmlWriterForPage(); // throws: no page open
// after
policy.AcquireXmlWriterForFixedPage();
XmlWriter w = policy.AcquireXmlWriterForPage();
... w.WriteRaw(...);
policy.ReleaseXmlWriterForPage();
policy.ReleaseXmlWriterForFixedPage();
Defensive patterns

Strategy: validation

Validate before calling

if (!pageOpen) throw new InvalidOperationException("Open a FixedPage (AcquireXmlWriterForFixedPage) before AcquireXmlWriterForPage");

Type guard

static bool PageOpen(XpsOMPackagingPolicy p) => p.CurrentFixedPageUri != null;

Try / catch

try { var w = policy.AcquireXmlWriterForPage(); }
catch (InvalidOperationException ex) { log("Page writer uninitialized — wrong lifecycle order", ex); }

Prevention

When it happens

Trigger: Calling AcquireXmlWriterForPage outside of an open FixedPage — i.e. before AcquireXmlWriterForFixedPage or after ReleaseXmlWriterForFixedPage closed the page.

Common situations: Custom serialization code acquiring page-content writers in the wrong order; writing after page release; calling the policy from a different code path than the standard XpsSerializationManager flow.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f0cccb72c2423a11. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/XpsOMPackagingPolicy.cs:344

        /// </summary>
        public
        override
        void
        RelateRestrictedFontToCurrentDocument(
            Uri targetUri
            )
        {
            return;
        }

        public
        override
        XmlWriter
        AcquireXmlWriterForPage()
        {
            if (_currentFixedPageXmlWriter == null)
            {
                throw new InvalidOperationException("CurrentFixedPageWriter uninitialized");
            }
            return _currentPageContentXmlWriter;
        }

        public
        override
        XmlWriter
        AcquireXmlWriterForResourceDictionary()
        {
            if (_currentFixedPageXmlWriter == null)
            {
                throw new InvalidOperationException("CurrentFixedPageWriter uninitialized");
            }
            return _currentResourceXmlWriter;
        }

        public
        override

View on GitHub (pinned to 81131a70a4)