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

  1. Stop using the collator after Close; obtain a fresh collator from the serializer for new content.
  2. Ensure Close/Cancel is called only once and track the closed state.
  3. 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

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


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)