dotnet/wpf · error · ApplicationException

SR.DocumentReferenceHasInvalidDocument

Error message

SR.DocumentReferenceHasInvalidDocument

What it means

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.

Solutions

  1. Ensure each DocumentReference.Source resolves to a valid FixedDocument before adding it to the sequence
  2. Check DocumentReference.IsInitialized/Document is a FixedDocument prior to adding
  3. Fix broken URIs and include XPS parts in the build output
  4. Load the document eagerly (GetDocument) and handle load failures before adding the reference

Example fix

// before
refs.Add(new DocumentReference { Source = brokenUri });
// after
var dr = new DocumentReference { Source = validUri };
if (dr.GetDocument() is FixedDocument) refs.Add(dr);
Defensive patterns

Strategy: validation

Validate before calling

var doc = docRef.GetDocument(); if (doc is not FixedDocument) throw new InvalidOperationException("DocumentReference does not resolve to a FixedDocument");

Type guard

static bool IsValidDocRef(DocumentReference dr) => dr != null && dr.GetDocument() is FixedDocument;

Try / catch

try { refs.Add(docRef); } catch (ApplicationException ex) { log.Error($"Invalid document for {docRef.Source}", ex); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/b2fc6608bf34a63f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequence.cs:731

            if (args.Action == NotifyCollectionChangedAction.Add)
            {
                if (args.NewItems.Count != 1)
                {
                    throw new NotSupportedException(SR.RangeActionsNotSupported);
                }
                else
                {
                    // get the affected item
                    object item = args.NewItems[0];

                    DocumentsTrace.FixedDocumentSequence.Content.Trace($"_OnCollectionChange: Add {item.GetHashCode()}");
                    AddLogicalChild(item);

                    int pageCount = this.PageCount;
                    DynamicDocumentPaginator paginator = GetPaginator((DocumentReference)item);
                    if (paginator == null)
                    {
                        throw new ApplicationException(SR.DocumentReferenceHasInvalidDocument);
                    }

                    int addedPages = paginator.PageCount;
                    int firstPage = pageCount - addedPages;

                    if (addedPages > 0)
                    {
                        DocumentsTrace.FixedDocumentSequence.Content.Trace($"_OnCollectionChange: Add with IDP {paginator.GetHashCode()}");
                        _paginator.NotifyPaginationProgress(new PaginationProgressEventArgs(firstPage, addedPages));
                        _paginator.NotifyPagesChanged(new PagesChangedEventArgs(firstPage, addedPages));
                    }
                }
            }
            else
            {
                throw new NotSupportedException(SR.Format(SR.UnexpectedCollectionChangeAction, args.Action));
            }
        }

View on GitHub (pinned to 81131a70a4)