dotnet/wpf · error · ArgumentException

SR.UriNotAbsolute

Error message

SR.UriNotAbsolute

What it means

The FontFamily(Uri baseUri, string familyName) constructor throws this ArgumentException when baseUri is non-null but relative (IsAbsoluteUri is false). A relative base URI cannot resolve font family references, so the library rejects it up front.

Solutions

  1. Pass an absolute Uri (e.g. new Uri(packUriString, UriKind.Absolute) or a file:/// URI).
  2. Resolve relative paths against a known absolute base before constructing the FontFamily.
  3. Pass null for baseUri if you intend a machine-fonts lookup, instead of a relative Uri.

Example fix

// before
var ff = new FontFamily(new Uri("fonts/", UriKind.Relative), "MyFont#Custom");
// after
var baseUri = new Uri("pack://application:,,,/MyApp;component/fonts/");
var ff = new FontFamily(baseUri, "MyFont#Custom");
Defensive patterns

Strategy: validation

Validate before calling

if (baseUri != null && !baseUri.IsAbsoluteUri)
    throw new ArgumentException("baseUri must be absolute", nameof(baseUri));

Type guard

bool IsValidFontBase(Uri u) => u == null || u.IsAbsoluteUri;

Try / catch

try { var ff = new FontFamily(baseUri, name); }
catch (ArgumentException) { /* baseUri was relative; resolve and retry */ }

Prevention

When it happens

Trigger: Calling new FontFamily(someRelativeUri, "Arial#Custom") where someRelativeUri was created with UriKind.Relative, or a Uri built from a path without a scheme.

Common situations: Building the base URI from a relative app path like "fonts/" without combining it with the application's Pack URI or absolute base directory.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FontFamily.cs:71

        /// of which may be either a regular family name string (e.g., "Arial") or a URI
        /// (e.g., "file:///c:/windows/fonts/#Arial").</param>
        public FontFamily(string familyName) : this(null, familyName)
        {}

        /// <summary>
        /// Constructs FontFamily from a string and an optional base URI.
        /// </summary>
        /// <param name="baseUri">Specifies the base URI used to resolve family names, typically 
        /// the URI of the document or element that refers to the font family. Can be null.</param>
        /// <param name="familyName">Specifies one or more comma-separated family names, each
        /// of which may be either a regular family name string (e.g., "Arial") or a URI
        /// (e.g., "file:///c:/windows/fonts/#Arial").</param>
        public FontFamily(Uri baseUri, string familyName)
        {
            ArgumentNullException.ThrowIfNull(familyName);

            if (baseUri != null && !baseUri.IsAbsoluteUri)
                throw new ArgumentException(SR.UriNotAbsolute, nameof(baseUri));

            _familyIdentifier = new FontFamilyIdentifier(familyName, baseUri);
        }

        internal FontFamily(FontFamilyIdentifier familyIdentifier)
        {
            _familyIdentifier = familyIdentifier;
        }

        /// <summary>
        /// Construct an anonymous font family, i.e., a composite font that is created
        /// programatically instead of referenced by name or URI.
        /// </summary>
        public FontFamily()
        {
            _familyIdentifier = new FontFamilyIdentifier(null, null);
            _firstFontFamily = new CompositeFontFamily();
        }

View on GitHub (pinned to 81131a70a4)