dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

Once XmlNamespaceMapping.Uri has a value, reassigning it during a later initialization pass with a different URI throws an InvalidOperationException with SR.PropertyIsImmutable. Prefix and Uri are both write-once to keep namespace mappings consistent.

Solutions

  1. Instantiate a fresh XmlNamespaceMapping for each prefix/uri pair.
  2. Assign Uri exactly once per instance.
  3. Remove the old mapping from the XmlNamespaceMappingCollection and add a new one instead of mutating.

Example fix

// before
mapping.Uri = new Uri("http://old.namespace");
mapping.Uri = new Uri("http://new.namespace"); // throws
// after
collection.Remove(mapping);
var newMapping = new XmlNamespaceMapping("ns", new Uri("http://new.namespace"));
collection.Add(newMapping);
Defensive patterns

Strategy: validation

Validate before calling

if (existing.Uri != null && existing.Uri != newUri)
    throw new InvalidOperationException("Uri is immutable; create a new mapping");

Try / catch

try { mapping.Uri = newUri; }
catch (InvalidOperationException ex) { log.Error("Uri already set; create a new XmlNamespaceMapping", ex); }

Prevention

When it happens

Trigger: Within BeginInit scope, setting Uri when _uri is already non-null and different (reusing a mapping instance for a second namespace).

Common situations: Sharing one mapping object across multiple collections/entries; trying to retarget an existing mapping to a new namespace URI.

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


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

Appendix: source

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

                _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>
        public static bool operator == (XmlNamespaceMapping mappingA, XmlNamespaceMapping mappingB)
        {

View on GitHub (pinned to 81131a70a4)