{"record":{"id":"86783bca9eeef639","repo":"dotnet/wpf","slug":"sr-propertyisinitializeonly-formatted-with-uri-type-name","errorCode":null,"errorMessage":"SR.PropertyIsInitializeOnly (formatted with \"Uri\", type name)","messagePattern":"SR\\.PropertyIsInitializeOnly \\(formatted with \"Uri\", type name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMapping.cs","lineNumber":65,"sourceCode":"                if (_prefix != null && _prefix != value)\n                    throw new InvalidOperationException(SR.Format(SR.PropertyIsImmutable, \"Prefix\", this.GetType().Name));\n\n                _prefix = value;\n            }\n        }\n\n        /// <summary>\n        /// The Uri to be used for this Namespace,\n        /// can be declared as an attribute or as the\n        /// TextContent of the XmlNamespaceMapping markup tag\n        /// </summary>\n        public Uri Uri\n        {\n            get { return _uri; }\n            set\n            {\n                if (!_initializing)\n                    throw new InvalidOperationException(SR.Format(SR.PropertyIsInitializeOnly, \"Uri\", this.GetType().Name));\n                if (_uri != null && _uri != value)\n                    throw new InvalidOperationException(SR.Format(SR.PropertyIsImmutable, \"Uri\", this.GetType().Name));\n\n                _uri = value;\n            }\n        }\n\n        /// <summary>\n        /// Equality comparison by value\n        /// </summary>\n        public override bool Equals(object obj)\n        {\n            return (this == (obj as XmlNamespaceMapping));  // call the == operator override\n        }\n\n        /// <summary>\n        /// Equality comparison by value\n        /// </summary>","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMapping.cs#L47-L83","documentation":"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.","triggerScenarios":"Setting mapping.Uri from code after construction outside BeginInit/EndInit, e.g. after the mapping has already been parsed from XAML or finalized.","commonSituations":"Programmatic construction of mappings without the ISupportInitialize protocol; attempting to update the namespace URI on a live binding after load.","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."],"exampleFix":"// before\nvar mapping = new XmlNamespaceMapping();\nmapping.Uri = new Uri(\"http://www.w3.org/2005/Atom\"); // throws\n// after\nvar mapping = new XmlNamespaceMapping(\"atom\", new Uri(\"http://www.w3.org/2005/Atom\"));","handlingStrategy":"validation","validationCode":"if (isInitialized) throw new InvalidOperationException(\"Uri cannot be set after initialization\");\n((ISupportInitialize)mapping).BeginInit();\nmapping.Uri = uri;\n((ISupportInitialize)mapping).EndInit();","typeGuard":null,"tryCatchPattern":"try { mapping.Uri = uri; }\ncatch (InvalidOperationException ex) { log.Error(\"Uri is initialize-only; use the constructor or BeginInit/EndInit\", ex); }","preventionTips":["Use the XmlNamespaceMapping(prefix, uri) constructor.","Set Uri only inside BeginInit/EndInit.","Never mutate mappings after the XmlDataProvider is initialized."],"tags":["wpf","xml","initialization","immutable-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}