dotnet/wpf · error · ArgumentException
URI must be absolute. Relative URIs are not supported.
Error message
URI must be absolute. Relative URIs are not supported.
What it means
PreloadedPackages keys its package registry by full package Uri; ValidateUriKey rejects null and any relative Uri, throwing ArgumentException (SR.UriMustBeAbsolute) with the 'uri' parameter name. This library throws it because only absolute package URIs can unambiguously identify a package store key.
Solutions
- Pass an absolute package URI, e.g. new Uri("pack://application:,,,/", UriKind.Absolute) or PackUriHelper.Create(partUri) results
- Resolve relative URIs against a known base: new Uri(baseUri, relativePath)
- Check uri.IsAbsoluteUri before calling the API and fail fast with a clearer message
Example fix
// before
packages.AddPackage(new Uri("/app.xps", UriKind.Relative), pkg);
// after
var absUri = new Uri("pack://application:,,,/app.xps", UriKind.Absolute);
packages.AddPackage(System.IO.Packaging.PackUriHelper.GetPackageUri(absUri), pkg); Defensive patterns
Strategy: validation
Validate before calling
if (uri == null || !uri.IsAbsoluteUri)
throw new ArgumentException("Package key must be an absolute URI", nameof(uri));
preloadedPackages.AddPackage(uri, package); Type guard
static bool IsAbsolutePackageUri(Uri u) => u != null && u.IsAbsoluteUri;
Try / catch
try { PreloadedPackages.AddPackage(uri, pkg); } catch (ArgumentException ex) when (ex.ParamName == "uri") { uri = new Uri(packBaseUri, uri); PreloadedPackages.AddPackage(uri, pkg); } Prevention
- Create package URIs with PackUriHelper.Create or absolute pack:// URIs only
- Validate uri.IsAbsoluteUri at configuration load time
- Resolve relative config paths against a documented base URI before use
When it happens
Trigger: Calling PreloadedPackages.GetPackage, AddPackage, or RemovePackage with a relative Uri such as new Uri("/myPackage.xps", UriKind.Relative) or a default-initialized Uri.
Common situations: Building package URIs from user or config strings that omit the scheme; switching from PackUriHelper-created absolute URIs to hand-built ones; passing pack part URIs that lost their base when resolved.
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
- ' ' is not a valid value for ' '.
- A read or write operation references a location outside the…
- Document PackagePart URI is not valid.
- Specified argument was out of the range of valid values.
- SR.AbsoluteUriNotAllowed
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/126ec6f7721c43bc.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/PreloadedPackages.cs:154
}
}
// Null the instance. Similar to Dispose, but not quite.
internal static void Clear()
{
lock (_globalLock)
{
_packagePairs = null;
}
}
private static void ValidateUriKey(Uri uri)
{
ArgumentNullException.ThrowIfNull(uri);
if (!uri.IsAbsoluteUri)
{
throw new ArgumentException(SR.UriMustBeAbsolute, nameof(uri));
}
}
#endregion Internal Methods
/// <summary>
/// Package-bool pair where the bool represents the thread-safety status of the package
/// </summary>
private class PackageThreadSafePair
{
//------------------------------------------------------
//
// Internal Constructors
//
//------------------------------------------------------
internal PackageThreadSafePair(Package package, bool threadSafe)
{
Invariant.Assert(package != null);View on GitHub (pinned to 81131a70a4)