dotnet/wpf · error · ApplicationException

SR.DocumentReferenceUnsupportedMimeType

Error message

SR.DocumentReferenceUnsupportedMimeType

What it means

_LoadDocument inspects the MIME type returned with the document stream. If it is neither XPS package MIME nor BAML MIME, it throws ApplicationException with SR.DocumentReferenceUnsupportedMimeType. The DocumentReference only knows how to load XPS (FixedDocument) and BAML content.

Solutions

  1. Point DocumentReference.Source at a real FixedDocument XPS part, not a container (.xps zip) or page sequence
  2. Fix the server's MIME mapping so the XPS part is served with application/vnd.ms-package.xps-fixeddocument
  3. Ensure the URI is not hitting an error/HTML page (verify by requesting the URL directly)
  4. If loading a whole .xps package, open the package and reference the FixedDocument inside instead

Example fix

// before
docReference.Source = new Uri("pack://application:,,,/report.xps"); // zip container, wrong MIME
// after
docReference.Source = new Uri("pack://application:,,,/report.xps/Documents/1/FixedDocument.fdoc");
Defensive patterns

Strategy: validation

Validate before calling

if (uri.IsFile && !uri.LocalPath.EndsWith(".fdoc", StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException("Point DocumentReference at a FixedDocument .fdoc part");

Try / catch

try { var doc = docRef.GetDocument(); } catch (ApplicationException ex) { log.Error($"Unsupported MIME for {docRef.Source}", ex); }

Prevention

When it happens

Trigger: DocumentReference.Source points at a resource whose server-reported Content-Type is neither application/vnd.ms-package.xps-fixeddocument nor the BAML MIME — e.g. HTML error pages, plain files, or a misconfigured server Content-Type mapping.

Common situations: Web server returning text/html for a missing file (soft 404) instead of the XPS file; IIS MIME map missing for .xps; pointing DocumentReference at a .fdseq or arbitrary file rather than a FixedDocument part.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentReference.cs:294

                }

                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
                {
                    throw new ApplicationException(SR.DocumentReferenceUnsupportedMimeType);
                }
                idp.DocumentReference = this;
            }

           return idp;
        }
        #endregion Private Methods

        //--------------------------------------------------------------------
        //
        // Private Fields
        //
        //---------------------------------------------------------------------

        #region Private Fields
        private FixedDocument _doc;
        private FixedDocument _docIdentity;     // used to cache the identity of the IDF so the IDF knows where it come from.
        #endregion Private Fields

View on GitHub (pinned to 81131a70a4)