{"record":{"id":"c8c7f797f1de9247","repo":"dotnet/wpf","slug":"printticket-was-already-committed-xpsfixedpagereaderwriter","errorCode":null,"errorMessage":"PrintTicket was already committed.","messagePattern":"PrintTicket was already committed\\.","errorType":"exception","errorClass":"XpsPackagingException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedPageReaderWriter.cs","lineNumber":509,"sourceCode":"        /// <exception cref=\"SR.ReachPackaging_PrintTicketAlreadyCommitted\">part is null.</exception>\n        /// <exception cref=\"SR.ReachPackaging_NotAPrintTicket\">part is null.</exception>\n        public PrintTicket PrintTicket\n        {\n            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.","sourceCodeStart":491,"sourceCodeEnd":527,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedPageReaderWriter.cs#L491-L527","documentation":"XpsPackagingException thrown from the PrintTicket setter of XpsFixedPageReaderWriter when PrintTicket is set a second time after it has already been committed to the package. The XPS writer model allows a page's PrintTicket to be assigned exactly once; after commit it is written as a part and the assignment is immutable. This enforces package consistency between the page and its ticket part.","triggerScenarios":"Assigning the PrintTicket property twice on an IXpsFixedPageReaderWriter where the first assignment completed the commit — typically pageReader.PrintTicket = ticket; ...; pageReader.PrintTicket = otherTicket; while writing an XPS document.","commonSituations":"Code paths that retry page writing and reassign the ticket on a retry; loops that set defaults for all pages and then set page-specific tickets; frameworks that set a ticket per-property-update.","solutions":["Set PrintTicket exactly once per page reader, before Commit()ing the page.","If the ticket must change, create a new page (or a new document) and write it with the corrected ticket instead of reassigning.","Restructure code so the final ticket is computed before the first assignment (e.g. resolve defaults up front).","Track commitment state in your own code (bool ticketSet) to guard reassignment.","Wrap in try-catch for XpsPackagingException to detect double-commit during batch generation."],"exampleFix":"// before\npageWriter.PrintTicket = defaultTicket;\nif (userOverride != null) pageWriter.PrintTicket = userOverride; // throws\n// after\nPrintTicket final = userOverride ?? defaultTicket;\npageWriter.PrintTicket = final; // set once\npageWriter.Commit();","handlingStrategy":"validation","validationCode":"// Guard before assigning\nbool canSetTicket = true; // track locally\nif (ticketAlreadySet.ContainsKey(pageWriter))\n    throw new InvalidOperationException(\"PrintTicket already committed for this page\");\n// else assign once and record\npageWriter.PrintTicket = ticket;\nticketAlreadySet[pageWriter] = true;","typeGuard":null,"tryCatchPattern":"try\n{\n    pageWriter.PrintTicket = finalTicket;\n}\ncatch (XpsPackagingException ex) when (ex.Message.Contains(\"already committed\"))\n{\n    log.Warn(\"PrintTicket reassignment ignored; first ticket retained.\");\n}","preventionTips":["Assign PrintTicket exactly once per page, computing the final value before the first set.","Centralize ticket assignment in a single SetPageTicket helper that tracks state.","Never set the ticket in retry loops; recreate the page on retry instead.","Commit pages only after the ticket is final."],"tags":["xps","wpf","printticket","double-commit"],"backgroundTag":"invalid-state-transition","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"}