dotnet/wpf · error · XpsSerializationException

SR.ReachSerialization_TargetNotPrintTicket

Error message

SR.ReachSerialization_TargetNotPrintTicket

What it means

ReachPrintTicketSerializer.SerializeObject throws XpsSerializationException(SR.ReachSerialization_TargetNotPrintTicket) when the object passed for serialization is null or not a PrintTicket (it is cast/as-cast to PrintTicket and null-checked). This serializer only persists PrintTicket objects; anything else — including null — is rejected with this meaningful exception.

Solutions

  1. Pass a non-null PrintTicket instance to SerializeObject
  2. Check upstream construction — a failed PrintTicket creation silently returned null
  3. Use the correct serializer for other object types (FixedPage/FixedDocument serializers)
  4. Guard with a type check before invoking the serializer

Example fix

// before
printTicketSerializer.SerializeObject(queue); // not a PrintTicket
// after
if (target is PrintTicket pt)
    printTicketSerializer.SerializeObject(pt);
else
    throw new ArgumentException("Expected a PrintTicket");
Defensive patterns

Strategy: validation

Validate before calling

if (obj is not PrintTicket pt) throw new ArgumentException("Expected PrintTicket");

Type guard

bool IsPrintTicket(object o) => o is PrintTicket;

Try / catch

try { printTicketSerializer.SerializeObject(obj); } catch (XpsSerializationException ex) { /* target was not a PrintTicket */ }

Prevention

When it happens

Trigger: Calling ReachPrintTicketSerializer.SerializeObject(obj) where obj is null, or is not a PrintTicket (e.g. a PrintQueue, a raw XML stream, or another document part).

Common situations: Manual XPS packaging code that passes the wrong part of the document to the print-ticket serializer; a print ticket failed to construct upstream and null propagated; confusion between PrintTicket and FixedPage/FixedDocument serializers.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachPrintTicketSerializer.cs:64

        /// </summary>
        /// <param name="serializedObject">
        /// Instance of object to be serialized.
        /// </param>
        public
        override
        void
        SerializeObject(
            object serializedObject
            )
        {
            PrintTicket printTicket = serializedObject as PrintTicket;

            if (printTicket == null)
            {
                //
                // Throw a meaningful exception
                //
                throw new XpsSerializationException(SR.ReachSerialization_TargetNotPrintTicket);
            }

            //  The new class XpsOMSerializationManager now also interacts with this class
            // the cast below is shorthand for cast to either XpsSerializationManager or XpsOMSerializationManager
            // we want this to throw an InvalidCastException if it fails to mantain compatibility.
            if ((IXpsSerializationManager)SerializationManager != null)
            {
                SerializationManager.
                     PackagingPolicy.PersistPrintTicket(printTicket);
            }
        }

        #endregion Public Methods

        #region Internal Methods

        /// <summary>
        /// The main method that is called to serialize the PrintTicket

View on GitHub (pinned to 81131a70a4)