dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

Once XmlNamespaceMapping.Prefix has been set (during initialization), it cannot be changed: a second assignment with a different value throws an InvalidOperationException with SR.PropertyIsImmutable. This keeps the mapping stable while the XML data binding is active.

Solutions

  1. Create a new XmlNamespaceMapping instance instead of mutating an existing one.
  2. Ensure the same Prefix value is assigned only once per instance.
  3. If the prefix must change, rebuild the mapping and re-add it to the collection.

Example fix

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

Strategy: validation

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: Inside a BeginInit scope, assigning Prefix a value when _prefix is already non-null and different from the new value (e.g. reusing and mutating a parsed mapping object).

Common situations: Reusing a single XmlNamespaceMapping instance across several XmlNamespaceMappingCollection entries with different prefixes; attempting to hot-swap the prefix on a live binding.

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

Appendix: source

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

        /// </summary>
        public XmlNamespaceMapping(string prefix, Uri uri)
        {
            _prefix = prefix;
            _uri = uri;
        }

        /// <summary>
        /// The prefix to be used for this Namespace
        /// </summary>
        public string Prefix
        {
            get { return _prefix; }
            set
            {
                if (!_initializing)
                    throw new InvalidOperationException(SR.Format(SR.PropertyIsInitializeOnly, "Prefix", this.GetType().Name));
                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)

View on GitHub (pinned to 81131a70a4)