dotnet/wpf · error · XpsPackagingException

Property is not a valid instance of PrintTicket.

Error message

Property is not a valid instance of PrintTicket.

What it means

The PrintTicket setter of XpsFixedDocumentSequenceReaderWriter throws XpsPackagingException (ReachPackaging_NotAPrintTicket) when the assigned value's runtime type is not exactly System.Printing.PrintTicket. Subclasses and foreign types are rejected so the ticket can be safely cloned (_printTicket = value.Clone()) and serialized.

Solutions

  1. Assign an exact System.Printing.PrintTicket instance.
  2. Round-trip a subclass through its XML (GetXmlStream) into a base PrintTicket before assigning.
  3. Correct the producing code (binder, deserializer, DI factory) to emit the exact type.

Example fix

// before
object value = settings["Ticket"];
seq.PrintTicket = (PrintTicketSubclass)value; // throws
// after
var exact = new PrintTicket(((PrintTicket)value).GetXmlStream());
seq.PrintTicket = exact;
Defensive patterns

Strategy: type-guard

Validate before calling

if (value != null && value.GetType() != typeof(PrintTicket))
    value = new PrintTicket(((PrintTicket)value).GetXmlStream());

Type guard

bool IsExactPrintTicket(object v) => v is PrintTicket && v.GetType() == typeof(PrintTicket);

Try / catch

try { seq.PrintTicket = ticket; }
catch (XpsPackagingException) { seq.PrintTicket = new PrintTicket(ticket.GetXmlStream()); }

Prevention

When it happens

Trigger: Assigning a PrintTicket subclass or a wrong-typed object to XpsFixedDocumentSequenceReaderWriter.PrintTicket; value != null but value.GetType() != typeof(PrintTicket).

Common situations: Derived PrintTicket classes from custom printing frameworks; loosely-typed values from configuration or UI binding; deserializers returning dynamic/subclass instances.

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/d2bae4c7f7efc89d. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentSequenceReaderWriter.cs:244

            get
            {
                if( _printTicket == null )
                {
                    _printTicket = CurrentXpsManager.EnsurePrintTicket(Uri);
                }
                return _printTicket;
            }
            set
            {
                if(value != null)
                {
                    if (_isPrintTicketCommitted)
                    {
                        throw new XpsPackagingException(SR.ReachPackaging_PrintTicketAlreadyCommitted);
                    }
                    if (!value.GetType().Equals(typeof(PrintTicket)))
                    {
                        throw new XpsPackagingException(SR.ReachPackaging_NotAPrintTicket);
                    }
                    _printTicket = value.Clone();
                }
                else
                {
                    _printTicket = null;
                }
            }
        }

        /// <summary>
        /// Gets a collection of all fixed documents that are contained within
        /// this document sequence.
        /// </summary>
        /// <value>
        /// Value is a Collection containing IXpsFixedDocumentReader interfaces.
        /// </value>
        /// <remarks>

View on GitHub (pinned to 81131a70a4)