{"record":{"id":"d2bae4c7f7efc89d","repo":"dotnet/wpf","slug":"property-is-not-a-valid-instance-of-printticket","errorCode":null,"errorMessage":"Property is not a valid instance of PrintTicket.","messagePattern":"Property is not a valid instance of PrintTicket\\.","errorType":"exception","errorClass":"XpsPackagingException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentSequenceReaderWriter.cs","lineNumber":244,"sourceCode":"            get\n            {\n                if( _printTicket == null )\n                {\n                    _printTicket = CurrentXpsManager.EnsurePrintTicket(Uri);\n                }\n                return _printTicket;\n            }\n            set\n            {\n                if(value != null)\n                {\n                    if (_isPrintTicketCommitted)\n                    {\n                        throw new XpsPackagingException(SR.ReachPackaging_PrintTicketAlreadyCommitted);\n                    }\n                    if (!value.GetType().Equals(typeof(PrintTicket)))\n                    {\n                        throw new XpsPackagingException(SR.ReachPackaging_NotAPrintTicket);\n                    }\n                    _printTicket = value.Clone();\n                }\n                else\n                {\n                    _printTicket = null;\n                }\n            }\n        }\n\n        /// <summary>\n        /// Gets a collection of all fixed documents that are contained within\n        /// this document sequence.\n        /// </summary>\n        /// <value>\n        /// Value is a Collection containing IXpsFixedDocumentReader interfaces.\n        /// </value>\n        /// <remarks>","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentSequenceReaderWriter.cs#L226-L262","documentation":"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.","triggerScenarios":"Assigning a PrintTicket subclass or a wrong-typed object to XpsFixedDocumentSequenceReaderWriter.PrintTicket; value != null but value.GetType() != typeof(PrintTicket).","commonSituations":"Derived PrintTicket classes from custom printing frameworks; loosely-typed values from configuration or UI binding; deserializers returning dynamic/subclass instances.","solutions":["Assign an exact System.Printing.PrintTicket instance.","Round-trip a subclass through its XML (GetXmlStream) into a base PrintTicket before assigning.","Correct the producing code (binder, deserializer, DI factory) to emit the exact type."],"exampleFix":"// before\nobject value = settings[\"Ticket\"];\nseq.PrintTicket = (PrintTicketSubclass)value; // throws\n// after\nvar exact = new PrintTicket(((PrintTicket)value).GetXmlStream());\nseq.PrintTicket = exact;","handlingStrategy":"type-guard","validationCode":"if (value != null && value.GetType() != typeof(PrintTicket))\n    value = new PrintTicket(((PrintTicket)value).GetXmlStream());","typeGuard":"bool IsExactPrintTicket(object v) => v is PrintTicket && v.GetType() == typeof(PrintTicket);","tryCatchPattern":"try { seq.PrintTicket = ticket; }\ncatch (XpsPackagingException) { seq.PrintTicket = new PrintTicket(ticket.GetXmlStream()); }","preventionTips":["Pass only exact PrintTicket instances to XPS APIs.","Normalize tickets to the base type at API boundaries.","Test any code path that produces tickets dynamically."],"tags":["wpf","xps","printticket","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}