{"record":{"id":"b2fc6608bf34a63f","repo":"dotnet/wpf","slug":"sr-documentreferencehasinvaliddocument","errorCode":null,"errorMessage":"SR.DocumentReferenceHasInvalidDocument","messagePattern":"SR\\.DocumentReferenceHasInvalidDocument","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequence.cs","lineNumber":731,"sourceCode":"            if (args.Action == NotifyCollectionChangedAction.Add)\n            {\n                if (args.NewItems.Count != 1)\n                {\n                    throw new NotSupportedException(SR.RangeActionsNotSupported);\n                }\n                else\n                {\n                    // get the affected item\n                    object item = args.NewItems[0];\n\n                    DocumentsTrace.FixedDocumentSequence.Content.Trace($\"_OnCollectionChange: Add {item.GetHashCode()}\");\n                    AddLogicalChild(item);\n\n                    int pageCount = this.PageCount;\n                    DynamicDocumentPaginator paginator = GetPaginator((DocumentReference)item);\n                    if (paginator == null)\n                    {\n                        throw new ApplicationException(SR.DocumentReferenceHasInvalidDocument);\n                    }\n\n                    int addedPages = paginator.PageCount;\n                    int firstPage = pageCount - addedPages;\n\n                    if (addedPages > 0)\n                    {\n                        DocumentsTrace.FixedDocumentSequence.Content.Trace($\"_OnCollectionChange: Add with IDP {paginator.GetHashCode()}\");\n                        _paginator.NotifyPaginationProgress(new PaginationProgressEventArgs(firstPage, addedPages));\n                        _paginator.NotifyPagesChanged(new PagesChangedEventArgs(firstPage, addedPages));\n                    }\n                }\n            }\n            else\n            {\n                throw new NotSupportedException(SR.Format(SR.UnexpectedCollectionChangeAction, args.Action));\n            }\n        }","sourceCodeStart":713,"sourceCodeEnd":749,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequence.cs#L713-L749","documentation":"When a DocumentReference is added to the sequence, _OnCollectionChanged obtains the reference's paginator; if GetPaginator returns null the referenced document is missing or invalid, and the handler throws ApplicationException with SR.DocumentReferenceHasInvalidDocument. The DocumentReference points to something that is not a loadable FixedDocument.","triggerScenarios":"Adding a DocumentReference whose Source is unresolvable, whose document failed to load, or whose referenced content is not a FixedDocument (e.g. cast failed to FixedDocument during load) at the time the collection-changed handler runs.","commonSituations":"Broken pack URIs in the DocumentReference; XPS documents not included in deployment; a reference initialized with an object that is not a FixedDocument; race where the document is not yet loaded when the reference is added.","solutions":["Ensure each DocumentReference.Source resolves to a valid FixedDocument before adding it to the sequence","Check DocumentReference.IsInitialized/Document is a FixedDocument prior to adding","Fix broken URIs and include XPS parts in the build output","Load the document eagerly (GetDocument) and handle load failures before adding the reference"],"exampleFix":"// before\nrefs.Add(new DocumentReference { Source = brokenUri });\n// after\nvar dr = new DocumentReference { Source = validUri };\nif (dr.GetDocument() is FixedDocument) refs.Add(dr);","handlingStrategy":"validation","validationCode":"var doc = docRef.GetDocument(); if (doc is not FixedDocument) throw new InvalidOperationException(\"DocumentReference does not resolve to a FixedDocument\");","typeGuard":"static bool IsValidDocRef(DocumentReference dr) => dr != null && dr.GetDocument() is FixedDocument;","tryCatchPattern":"try { refs.Add(docRef); } catch (ApplicationException ex) { log.Error($\"Invalid document for {docRef.Source}\", ex); }","preventionTips":["Validate Source resolves to a FixedDocument before adding","Include XPS parts in deployment output","Load documents eagerly and fail fast on broken references"],"tags":["wpf","xps","invalid-document"],"backgroundTag":"entity-not-found","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"}