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
- Set both Prefix and Uri before calling EndInit().
- Use the (string prefix, Uri uri) constructor so both are always supplied.
- 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
- Always set both Prefix and Uri before EndInit.
- Use the two-argument constructor to avoid partial initialization.
- Review code paths that conditionally skip property assignment.
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
- SR.PropertyMustHaveValue (formatted with "Uri", type name)
- SR.RequiresXmlNamespaceMappingUri
- " }} " element found. Expected fixed page element ( }} ).
- Bracket characters cannot be alpha-numeric or whitespace.
- Bracket characters cannot be one of the following: '=' …
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)