dotnet/wpf · error · ArgumentException

SR.UriMustBeAbsolute

Error message

SR.UriMustBeAbsolute

What it means

PackageStore.ValidatePackageUri is the shared guard used by GetPackage, AddPackage, and RemovePackage. It throws ArgumentNullException for a null uri and ArgumentException(SR.UriMustBeAbsolute) when the Uri is relative, because store keys are absolute pack URIs. All three public PackageStore methods funnel through this validation.

Solutions

  1. Pass an absolute pack URI, e.g. new Uri("pack://myapp:,,,/", UriKind.Absolute).
  2. Call PackUriHelper.Create(uriString) to normalize/validate the pack URI before store access.
  3. Guard with uri.IsAbsoluteUri before calling any PackageStore method.

Example fix

// before
PackageStore.GetPackage(new Uri("myapp:,,,/res")); // ArgumentException
// after
PackageStore.GetPackage(new Uri("pack://myapp:,,,/res", UriKind.Absolute));
Defensive patterns

Strategy: validation

Validate before calling

if (uri == null) throw new ArgumentNullException(nameof(uri));
if (!uri.IsAbsoluteUri) throw new ArgumentException("package URI must be absolute", nameof(uri));

Type guard

static bool IsValidPackageUri(Uri u) => u != null && u.IsAbsoluteUri;

Try / catch

try { pkg = PackageStore.GetPackage(uri); }
catch (ArgumentException ex) when (ex.ParamName == nameof(uri)) { uri = new Uri("pack://application:,,," + uri.OriginalString.TrimStart('/'), UriKind.Absolute); pkg = PackageStore.GetPackage(uri); }

Prevention

When it happens

Trigger: Passing a relative Uri (or null) to PackageStore.GetPackage, AddPackage, or RemovePackage.

Common situations: Storing pack keys as relative strings like "myapp:,,,/x" and constructing Uri without the pack:// scheme; URIs read from config that omit the authority prefix.

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/efc0cbe969647de7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/IO/Packaging/PackageStore.cs:144

            lock (_globalLock)
            {
                // If the key doesn't exist, it is no op
                _packages?.Remove(uri);
            }
        }

        #endregion Public Methods

        #region Private Methods

        private static void ValidatePackageUri(Uri uri)
        {
            ArgumentNullException.ThrowIfNull(uri);

            if (!uri.IsAbsoluteUri)
            {
                throw new ArgumentException(SR.UriMustBeAbsolute, nameof(uri));
            }
        }
        

        #endregion Private Methods
    
        #region Private Fields

        // We expect to have no more than 10 packages in the store
        //  per AppDomain for our scenarios
        // ListDictionary is the best fit for this scenarios; otherwise we should be using
        // Hashtable. HybridDictionary already has functionality of switching between
        //  ListDictionary and Hashtable depending on the size of the collection
        private static HybridDictionary _packages;
        private static readonly Object _globalLock;

        #endregion Private Fields
    }

View on GitHub (pinned to 81131a70a4)