dotnet/wpf · error · InvalidOperationException

SR.PropertyMustHaveValue (formatted with "Uri", type name)

Error message

SR.PropertyMustHaveValue (formatted with "Uri", type name)

What it means

EndInit also requires Uri: if _uri is null when the initialization window closes, XmlNamespaceMapping throws an InvalidOperationException with SR.PropertyMustHaveValue formatted with "Uri". A mapping must fully specify prefix and namespace URI to be usable.

Solutions

  1. Set both Prefix and Uri before EndInit().
  2. Prefer the two-argument constructor XmlNamespaceMapping(prefix, uri).
  3. Validate uri != null before entering BeginInit/EndInit.

Example fix

// before
mapping.BeginInit();
mapping.Prefix = "atom";
mapping.EndInit(); // throws
// after
mapping.BeginInit();
mapping.Prefix = "atom";
mapping.Uri = new Uri("http://www.w3.org/2005/Atom");
mapping.EndInit();
Defensive patterns

Strategy: validation

Validate before calling

if (uri == null) throw new ArgumentException("Uri required before EndInit", nameof(uri));
((ISupportInitialize)mapping).BeginInit();
mapping.Prefix = prefix;
mapping.Uri = uri;
((ISupportInitialize)mapping).EndInit();

Try / catch

try { ((ISupportInitialize)mapping).EndInit(); }
catch (InvalidOperationException ex) { log.Error("Mapping incomplete: " + ex.Message, ex); }

Prevention

When it happens

Trigger: Calling EndInit() after setting only Prefix (Uri left null), or completing initialization without setting either property.

Common situations: Forgetting the Uri when building mappings in code; a Uri variable that is null due to a failed parse earlier in the setup code.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMapping.cs:141

#region ISupportInitialize

        /// <summary>Begin Initialization</summary>
        void ISupportInitialize.BeginInit()
        {
            _initializing = true;
        }

        /// <summary>End Initialization, verify that internal state is consistent</summary>
        void ISupportInitialize.EndInit()
        {
            if (_prefix == null)
            {
                throw new InvalidOperationException(SR.Format(SR.PropertyMustHaveValue, "Prefix", this.GetType().Name));
            }
            if (_uri == null)
            {
                throw new InvalidOperationException(SR.Format(SR.PropertyMustHaveValue, "Uri", this.GetType().Name));
            }

            _initializing = false;
        }

#endregion ISupportInitialize

        private string _prefix;
        private Uri _uri;
        private bool _initializing;
    }
}

View on GitHub (pinned to 81131a70a4)