dotnet/wpf · error · ApplicationException
SR.DocumentReferenceNotFound
Error message
SR.DocumentReferenceNotFound
What it means
DocumentReference._LoadDocument downloads the document at its Source URI and throws ApplicationException with SR.DocumentReferenceNotFound when the web response stream comes back null. This means WpfWebRequestHelper could not obtain a stream for the URI, i.e. the resource was not found or the request failed. It guards FixedDocumentSequence/XPS loading against silent null documents.
Solutions
- Verify the DocumentReference.Source URI resolves to an existing document (fix the pack/file/http URI)
- Use an absolute, correctly-formed pack URI (e.g. pack://application:,,,/Documents/FixedDocumentSequence.fdseq) instead of a broken relative path
- Ensure the document file is included in project output (CopyToOutputDirectory) so it exists at runtime
- Check the hosting web server/IIS actually serves the resource (test the URL in a browser) and the app has network access
Example fix
// before
docReference.Source = new Uri("Doc/fixdoc.xps", UriKind.Relative);
// after
docReference.Source = new Uri("pack://application:,,,/Doc/fixdoc.xps", UriKind.Absolute); Defensive patterns
Strategy: validation
Validate before calling
bool docExists = uri != null && (uri.IsFile ? File.Exists(uri.LocalPath) : true); if (!docExists) throw new FileNotFoundException(uri?.ToString());
Try / catch
try { var doc = docRef.GetDocument(); } catch (ApplicationException ex) { log.Error($"Document at {docRef.Source} not found", ex); ShowFallbackDocument(); } Prevention
- Verify pack URIs with a browser or direct request before use
- Ship document files with the app and mark CopyToOutputDirectory
- Use absolute pack URIs to avoid relative-resolution surprises
- Handle load failures with a fallback UI
When it happens
Trigger: Calling DocumentReference.GetDocument (or idpReloaded reload flow) when the Source URI points to a missing file, a wrong path, an unreachable server, or a handler that returns an empty/failed response so CreateRequestAndGetResponseStream returns null.
Common situations: Typo in the XPS/document path or pack URI; document served from a web server that returns 404; relative BaseUri resolved incorrectly after moving the app; file removed or renamed after build; running with restricted network/permissions in partial trust.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- " }} " element found. Expected fixed page element ( }} ).
- ' ' ContentType is not valid.
- ' ' ContentType is not valid.
- Buffer address passed to GetText cannot be NULL.
- Cannot convert from type.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b347c04e68cc9010.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentReference.cs:275
// sync load a document
private FixedDocument _LoadDocument()
{
FixedDocument idp = null;
Uri uriToLoad = _ResolveUri();
if (uriToLoad != null)
{
// Package-boundary guard. Reject any URI that escapes the XPS package.
XpsLoadingContext.EnforcePackageRelativeUri(((IUriContext)this).BaseUri, uriToLoad);
ContentType mimeType = null;
Stream docStream = null;
docStream = WpfWebRequestHelper.CreateRequestAndGetResponseStream(uriToLoad, out mimeType);
if (docStream == null)
{
throw new ApplicationException(SR.DocumentReferenceNotFound);
}
ParserContext pc = new ParserContext
{
BaseUri = uriToLoad
};
if (BindUriHelper.IsXamlMimeType(mimeType))
{
XpsValidatingLoader loader = new XpsValidatingLoader();
idp = loader.Load(docStream, ((IUriContext)this).BaseUri, pc, mimeType) as FixedDocument;
}
else if (MS.Internal.MimeTypeMapper.BamlMime.AreTypeAndSubTypeEqual(mimeType))
{
idp = XamlReader.LoadBaml(docStream, pc, null, true) as FixedDocument;
}
else
{View on GitHub (pinned to 81131a70a4)