{"record":{"id":"9fe12cbe20bae5c0","repo":"dotnet/wpf","slug":"property-is-not-a-valid-instance-of-printticket-9fe12c","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/XpsFixedPageReaderWriter.cs","lineNumber":513,"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\n                    _printTicket = value.Clone();\n                }\n                else\n                {\n                    _printTicket = null;\n                }\n            }\n        }\n\n        /// <summary>\n        /// Gets a reference to the XmlWriter for the Metro part\n        /// that represents this fixed page within the package.\n        /// </summary>\n        public XmlWriter XmlWriter\n        {\n            get","sourceCodeStart":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedPageReaderWriter.cs#L495-L531","documentation":"XpsPackagingException thrown from the PrintTicket setter of XpsFixedPageReaderWriter when the assigned value's runtime type is not exactly System.Printing.PrintTicket (the check uses GetType().Equals, so subclasses fail too). The writer requires an exact PrintTicket instance because it clones the ticket to snapshot its settings. It guarantees the stored object exposes the full PrintTicket surface.","triggerScenarios":"Assigning any object that is not exactly PrintTicket to pageWriter.PrintTicket — e.g. a subclass of PrintTicket, an object from a different assembly/version of ReachFramework/PresentationCore, or an incorrectly cast value from deserialization.","commonSituations":"Custom PrintTicket-derived classes used for extra metadata; DLL version mismatches causing two distinct PrintTicket types in one AppDomain; dynamic/late-bound assignment from config that yields System.Object at runtime.","solutions":["Assign an instance of exactly System.Printing.PrintTicket; do not subclass it.","If you have a derived type, construct a new PrintTicket and copy needed properties (e.g. via GetXmlStream/Build a fresh ticket).","Verify assembly binding — ensure only one version of the WPF/Printing assemblies loads in the process.","Check the value's actual type at the assignment site (value.GetType()) when data comes from reflection or deserialization."],"exampleFix":"// before\nclass MyTicket : PrintTicket { } // subclass\npageWriter.PrintTicket = new MyTicket(...); // throws\n// after\nPrintTicket ticket = new PrintTicket(printQueue.DefaultPrintTicket.GetXmlStream());\nticket.PageOrientation = PageOrientation.Landscape;\npageWriter.PrintTicket = ticket; // exact type","handlingStrategy":"type-guard","validationCode":"// Verify exact runtime type before assigning\nif (value.GetType() != typeof(System.Printing.PrintTicket))\n    throw new ArgumentException(\"PrintTicket must be exactly System.Printing.PrintTicket\", nameof(value));","typeGuard":"static bool IsExactPrintTicket(object value) =>\n    value is PrintTicket && value.GetType() == typeof(PrintTicket);","tryCatchPattern":"try\n{\n    pageWriter.PrintTicket = candidate;\n}\ncatch (XpsPackagingException ex) when (ex.Message.Contains(\"PrintTicket\"))\n{\n    // rebuild a genuine PrintTicket from XML\n    var xml = GetTicketXml(candidate);\n    pageWriter.PrintTicket = new PrintTicket(new MemoryStream(xml));\n}","preventionTips":["Never subclass PrintTicket; extend behavior externally.","Build tickets with new PrintTicket(Stream) from ticket XML to guarantee the exact type.","Check assembly binding for duplicate WPF/Printing assembly versions loading into the AppDomain.","When data arrives via reflection/deserialization, assert value.GetType() == typeof(PrintTicket) before use."],"tags":["xps","wpf","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-21T21:30:21.729Z"}