dotnet/wpf · error · InvalidOperationException
SR.PropertyIsInitializeOnly (formatted with "Uri", type…
Error message
SR.PropertyIsInitializeOnly (formatted with "Uri", type name)
What it means
XmlNamespaceMapping.Uri is initialize-only: it may only be assigned during the BeginInit/EndInit initialization window. Assigning it after initialization throws an InvalidOperationException formatted with the property name and type name, same pattern as Prefix.
Solutions
- Define the mapping in XAML so initialization protocol is applied automatically.
- In code, wrap assignments in ((ISupportInitialize)mapping).BeginInit() ... EndInit().
- Use the constructor that accepts Prefix and Uri directly (public XmlNamespaceMapping(string prefix, Uri uri)) so initialization happens once.
Example fix
// before
var mapping = new XmlNamespaceMapping();
mapping.Uri = new Uri("http://www.w3.org/2005/Atom"); // throws
// after
var mapping = new XmlNamespaceMapping("atom", new Uri("http://www.w3.org/2005/Atom")); Defensive patterns
Strategy: validation
Validate before calling
if (isInitialized) throw new InvalidOperationException("Uri cannot be set after initialization");
((ISupportInitialize)mapping).BeginInit();
mapping.Uri = uri;
((ISupportInitialize)mapping).EndInit(); Try / catch
try { mapping.Uri = uri; }
catch (InvalidOperationException ex) { log.Error("Uri is initialize-only; use the constructor or BeginInit/EndInit", ex); } Prevention
- Use the XmlNamespaceMapping(prefix, uri) constructor.
- Set Uri only inside BeginInit/EndInit.
- Never mutate mappings after the XmlDataProvider is initialized.
When it happens
Trigger: Setting mapping.Uri from code after construction outside BeginInit/EndInit, e.g. after the mapping has already been parsed from XAML or finalized.
Common situations: Programmatic construction of mappings without the ISupportInitialize protocol; attempting to update the namespace URI on a live binding after load.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- SR.PropertyIsInitializeOnly (formatted with "Prefix", type…
- SR.Format(SR.PropertyIsInitializeOnly, "DataType"…
- SR.PropertyIsImmutable (formatted with "Prefix", type name)
- SR.PropertyIsImmutable (formatted with "Uri", type name)
- " }} " element found. Expected fixed page element ( }} ).
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/86783bca9eeef639.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMapping.cs:65
if (_prefix != null && _prefix != value)
throw new InvalidOperationException(SR.Format(SR.PropertyIsImmutable, "Prefix", this.GetType().Name));
_prefix = value;
}
}
/// <summary>
/// The Uri to be used for this Namespace,
/// can be declared as an attribute or as the
/// TextContent of the XmlNamespaceMapping markup tag
/// </summary>
public Uri Uri
{
get { return _uri; }
set
{
if (!_initializing)
throw new InvalidOperationException(SR.Format(SR.PropertyIsInitializeOnly, "Uri", this.GetType().Name));
if (_uri != null && _uri != value)
throw new InvalidOperationException(SR.Format(SR.PropertyIsImmutable, "Uri", this.GetType().Name));
_uri = value;
}
}
/// <summary>
/// Equality comparison by value
/// </summary>
public override bool Equals(object obj)
{
return (this == (obj as XmlNamespaceMapping)); // call the == operator override
}
/// <summary>
/// Equality comparison by value
/// </summary>View on GitHub (pinned to 81131a70a4)