{"record":{"id":"037983191b5d29fb","repo":"dotnet/wpf","slug":"sr-reachserialization-fixeddocumentinpage-037983","errorCode":null,"errorMessage":"SR.ReachSerialization_FixedDocumentInPage","messagePattern":"SR\\.ReachSerialization_FixedDocumentInPage","errorType":"exception","errorClass":"XpsSerializationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/XpsSerializationManager.cs","lineNumber":844,"sourceCode":"        AddRelationshipToCurrentPage(\n            Uri targetUri,\n            string relationshipName\n            )\n        {\n            _packagingPolicy?.RelateResourceToCurrentPage(targetUri, relationshipName);\n        }\n\n        internal\n        void\n        RegisterDocumentStart()\n        {\n            if( _documentStartState )\n            {\n                throw new XpsSerializationException(SR.ReachSerialization_FixedDocumentInDocument);\n            }\n            if( _pageStartState)\n            {\n                throw new XpsSerializationException(SR.ReachSerialization_FixedDocumentInPage);\n            }\n            //\n            // Entering Document  Started state\n            //\n            _documentStartState = true;\n            //\n            // Increment the number of documents serialized\n            //\n            _documentNumber += 1;\n            //\n            // Clearing the number of pages for this document\n            //\n            _pageNumber = 0;\n        }\n\n\n        internal\n        void","sourceCodeStart":826,"sourceCodeEnd":862,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/XpsSerializationManager.cs#L826-L862","documentation":"RegisterDocumentStart also rejects starting a document while a page is still open (_pageStartState). If a FixedPage has been started but not ended, starting a FixedDocument would illegally nest a document inside a page, so XpsSerializationException(SR.ReachSerialization_FixedDocumentInPage) is thrown.","triggerScenarios":"Calling RegisterDocumentStart after RegisterPageStart without RegisterPageEnd — e.g. a paginator that begins a new document mid-page, or missing/failed page-end cleanup on an exception path.","commonSituations":"Error-handling gaps where RegisterPageEnd is skipped after a serialization failure; per-page document rotation logic in custom XPS writers; async serialization callbacks firing out of order.","solutions":["Ensure RegisterPageEnd (and page commit) is always called before RegisterDocumentStart, including on error paths (try/finally).","Restructure pagination so documents and pages nest correctly: DocumentSequence > Document > Page.","Add assertions/logging around the register calls to catch ordering bugs early.","Use DocumentPaginator + XpsDocumentWriter to let WPF manage document/page sequencing."],"exampleFix":"// before\nmanager.RegisterPageStart();\nmanager.RegisterDocumentStart(); // document inside open page\n// after\nmanager.RegisterPageStart();\nmanager.RegisterPageEnd();\nmanager.RegisterDocumentEnd();\nmanager.RegisterDocumentStart();","handlingStrategy":"validation","validationCode":"if (pageOpen)\n    throw new InvalidOperationException(\"Cannot start a FixedDocument while a FixedPage is open; call RegisterPageEnd first.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    manager.RegisterDocumentStart();\n}\ncatch (System.Windows.Xps.XpsSerializationException ex) when (ex.Message.Contains(\"page\"))\n{\n    // close the dangling page/document chain before retrying\n    manager.RegisterPageEnd();\n    manager.RegisterDocumentEnd();\n    manager.RegisterDocumentStart();\n}","preventionTips":["Wrap page serialization in try/finally so RegisterPageEnd always runs, even on exceptions.","Nest calls in the correct order: sequence start > document start > page start/end > document end > sequence end.","Log each register call in debug builds to catch ordering violations early."],"tags":["wpf","xps","state-machine","serialization"],"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-22T01:17:13.364Z"}