dotnet/wpf · error · UriFormatException

SR.WrongFirstSegment

Error message

SR.WrongFirstSegment

What it means

BaseUriHelper.GetAssemblyNameAndPart parses the first segment of a pack application URI (the part after 'application:///' containing component segments) and throws UriFormatException(SR.WrongFirstSegment) when the segment count is not between 2 and 4. The URI's first segment must have the form componentname;componentkey;version with an optional extra part.

Solutions

  1. Correct the pack URI so the first segment has the expected form: componentname;componentkey;version (2-4 semicolon-separated parts).
  2. Use the standard 'pack://application:,,,/AssemblyName;component/Path' syntax without stray semicolons in the assembly portion.
  3. Validate the URI with Uri.TryCreate and inspect the absolute path before handing it to pack URI resolution.

Example fix

// before
var uri = new Uri("pack://application:,,,/a;b;c;d;e/Win.xaml", UriKind.Absolute); // 5 segments -> throws
// after
var uri = new Uri("pack://application:,,,/MyAssembly;v1.0.0.0;component/Win.xaml", UriKind.Absolute);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidPackAppFirstSegment(Uri u)
{
    if (u == null || !u.IsAbsoluteUri) return false;
    var seg = u.AbsolutePath.TrimStart('/');
    var parts = seg.Split(';');
    return parts.Length >= 2 && parts.Length <= 4;
}

Try / catch

try { var name = BaseUriHelper.GetAssemblyAndPartNameFromPackAppUri(uri); }
catch (UriFormatException ex) { /* log/repair the pack URI */ }

Prevention

When it happens

Trigger: Passing a pack://application:,,,/ URI whose first segment splits into fewer than 2 or more than 4 semicolon-delimited components, via GetAssemblyAndPartNameFromPackAppUri.

Common situations: Malformed pack URIs in XAML or code: wrong number of semicolon-separated assembly parts, typos like 'assembly;;' or extra segments, URIs copied from a different .NET/WPF version with different pack syntax expectations.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Navigation/BaseUriHelper.cs:274

                firstSegment = original.Substring(start, end - start);

                // The resource comes from dll
                if (firstSegment.EndsWith(COMPONENT, StringComparison.OrdinalIgnoreCase))
                {
                    partName = original.Substring(end + 1);
                    fHasComponent = true;
                }
            }

            if (fHasComponent)
            {
                string[] assemblyInfo = firstSegment.Split(COMPONENT_DELIMITER);

                int count = assemblyInfo.Length;

                if ((count > 4) || (count < 2))
                {
                    throw new UriFormatException(SR.WrongFirstSegment);
                }

                //
                // if the uri contains escaping character,
                // Convert it back to normal unicode string
                // so that the string as assembly name can be
                // recognized by Assembly.Load later.
                //
                assemblyName = Uri.UnescapeDataString(assemblyInfo[0]);

                for (int i = 1; i < count - 1; i++)
                {
                    if (assemblyInfo[i].StartsWith(VERSION, StringComparison.OrdinalIgnoreCase))
                    {
                        if (string.IsNullOrEmpty(assemblyVersion))
                        {
                            assemblyVersion = assemblyInfo[i].Substring(1);  // Get rid of the leading "v"
                        }

View on GitHub (pinned to 81131a70a4)