dotnet/wpf · error · ArgumentException

SR.NonPackAppAbsoluteUriNotAllowed

Error message

SR.NonPackAppAbsoluteUriNotAllowed

What it means

Application.GetResourceStream(Uri) accepts absolute Uris only when they are pack application URIs (BaseUriHelper.IsPackApplicationUri). Any other absolute scheme (http://, file://, etc.) throws ArgumentException with SR.NonPackAppAbsoluteUriNotAllowed. Relative Uris are always accepted and resolved against the app base.

Solutions

  1. Pass a relative Uri to the resource: new Uri("images/logo.png", UriKind.Relative)
  2. Use an absolute pack application Uri of the form pack://application:,,,/ResourceFile
  3. Download non-pack content with HttpClient/WebClient instead of GetResourceStream
  4. Guard with uri.IsAbsoluteUri && !BaseUriHelper.IsPackApplicationUri(uri) before calling

Example fix

// before
var sri = Application.GetResourceStream(new Uri("http://example.com/logo.png"));

// after
var uri = new Uri("images/logo.png", UriKind.Relative);
var sri = Application.GetResourceStream(uri);
Defensive patterns

Strategy: validation

Validate before calling

if (uri.IsAbsoluteUri && !BaseUriHelper.IsPackApplicationUri(uri))
    throw new ArgumentException("GetResourceStream requires a relative or pack://application Uri");

Type guard

static bool IsPackAppOrRelative(Uri u) => u != null && (!u.IsAbsoluteUri || BaseUriHelper.IsPackApplicationUri(u));

Try / catch

try { sri = Application.GetResourceStream(uri); }
catch (ArgumentException) { sri = Application.GetResourceStream(new Uri(ToRelativeResourcePath(uri), UriKind.Relative)); }

Prevention

When it happens

Trigger: Calling GetResourceStream(new Uri("http://example.com/img.png")) or file:///... ; also absolute pack://siteoforigin URIs (siteOfOrigin is not an application URI).

Common situations: Pointing resource loading at remote URLs and expecting GetResourceStream to fetch them; using siteoforigin pack URIs where application URIs are required; config storing full URLs instead of relative resource paths.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs:583

        ///         "pack://application:,,,/image/picture1.jpg"
        ///
        ///    Resource from a library assembly
        ///         "mylibrary;component/image/picture2.png" or
        ///         "pack://application:,,,/mylibrary;component/image/picture3.jpg"
        ///
        /// </summary>
        /// <param name="uriResource">the uri maps to the resource</param>
        /// <returns>PackagePart or null</returns>
        public static StreamResourceInfo GetResourceStream(Uri uriResource)
        {
            ArgumentNullException.ThrowIfNull(uriResource);

            if (uriResource.OriginalString == null)
                throw new ArgumentException(SR.Format(SR.ArgumentPropertyMustNotBeNull, "uriResource", "OriginalString"));

            if (uriResource.IsAbsoluteUri && !BaseUriHelper.IsPackApplicationUri(uriResource))
            {
                throw new ArgumentException(SR.NonPackAppAbsoluteUriNotAllowed);
            }

            ResourcePart part = GetResourceOrContentPart(uriResource) as ResourcePart;
            return (part == null) ? null : new StreamResourceInfo(part.GetSeekableStream(), part.ContentType);
        }

        /// <summary>
        /// Get PackagePart for a uri, the uri maps to a content file which is associated
        /// with the application assembly.
        ///
        /// If the Uri doesn't map to any content file, this method returns null.
        ///
        /// The accepted uri could be relative uri or pack://Application:,,,/ uri.
        ///
        ///   Such as
        ///         "image/picture1.jpg"
        ///    or
        ///        "pack://application:,,,/image/picture1.jpg"

View on GitHub (pinned to 81131a70a4)