dotnet/wpf · error · ArgumentException

SR.Format(SR.ArgumentPropertyMustNotBeNull, "uriResource"…

Error message

SR.Format(SR.ArgumentPropertyMustNotBeNull, "uriResource", "OriginalString")

What it means

Application.GetResourceStream(Uri) validates uriResource and throws ArgumentException (SR.ArgumentPropertyMustNotBeNull naming uriResource.OriginalString) when the Uri's OriginalString is null. GetResourceStream returns a StreamResourceInfo for an application resource and relies on a well-formed relative or pack-application Uri.

Solutions

  1. Create the Uri from a non-null string, e.g. new Uri("images/logo.png", UriKind.Relative)
  2. Guard the call: if (uri == null || uri.OriginalString == null) skip/throw a clearer error
  3. Fix the source that produced the degenerate Uri

Example fix

// before
Uri uri = LoadFromConfig(); // OriginalString == null
var sri = Application.GetResourceStream(uri);

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

Strategy: validation

Validate before calling

if (uri == null || uri.OriginalString == null)
    return null; // or throw a clearer error
var sri = Application.GetResourceStream(uri);

Type guard

static bool IsValidResourceUri(Uri u) => u?.OriginalString != null;

Try / catch

try { sri = Application.GetResourceStream(uri); }
catch (ArgumentException ex) { Log(ex); sri = null; }

Prevention

When it happens

Trigger: Calling GetResourceStream with a Uri instance whose OriginalString is null (e.g. from deserialization or an uninitialized property).

Common situations: Reading resource paths from settings that were never populated; a Uri produced by an external library with a null backing string.

Related errors


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

Appendix: source

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

        ///
        ///   Such as
        ///    Resource from Application assembly
        ///         "image/picture1.jpg" or
        ///         "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.
        ///

View on GitHub (pinned to 81131a70a4)