dotnet/wpf · error · InvalidOperationException

SR.PropertyMustHaveValue (formatted with "Prefix", type…

Error message

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

What it means

XmlNamespaceMapping's ISupportInitialize.EndInit validates that Prefix was actually set. If _prefix is still null when initialization finishes, it throws an InvalidOperationException stating the property must have a value. A mapping without a Prefix is meaningless for XML namespace resolution.

Solutions

  1. Set both Prefix and Uri before calling EndInit().
  2. Use the (string prefix, Uri uri) constructor so both are always supplied.
  3. Add a null check on mapping values before the EndInit call.

Example fix

// before
mapping.BeginInit();
mapping.Uri = nsUri;
mapping.EndInit(); // throws
// after
mapping.BeginInit();
mapping.Prefix = "atom";
mapping.Uri = nsUri;
mapping.EndInit();
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(prefix)) throw new ArgumentException("Prefix required before EndInit", nameof(prefix));
((ISupportInitialize)mapping).BeginInit();
mapping.Prefix = prefix;
((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 Uri (Prefix left null), or calling EndInit() without ever setting either property.

Common situations: Partial programmatic initialization — forgetting one of the two required properties; conditional code paths that skip the Prefix assignment.

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

Appendix: source

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

                return unchecked(hash + _uri.GetHashCode());
            else
                return hash;
        }

#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)