{"record":{"id":"4df0ad8f8b12bf77","repo":"dotnet/wpf","slug":"sr-cannotsetbaseuri","errorCode":null,"errorMessage":"SR.CannotSetBaseUri","messagePattern":"SR\\.CannotSetBaseUri","errorType":"exception","errorClass":"XamlObjectWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs","lineNumber":1669,"sourceCode":"                else if (prop == XamlLanguage.Base)\n                {\n                    Logic_CheckBaseUri(ctx, (string)value);\n                    ctx.BaseUri = new Uri((string)value);\n                    if (ctx.ParentInstance is not null)\n                    {\n                        Runtime.SetUriBase(ctx.ParentType, ctx.ParentInstance, ctx.BaseUri);\n                    }\n                }\n            }\n        }\n\n        private void Logic_CheckBaseUri(ObjectWriterContext ctx, string value)\n        {\n            // Make sure BaseUri is not already set and that we can only set BaseUri on the root element\n            // Depth > 2 because on the root element, SO/SM takes 1 slot, V is slot 2\n            if ((ctx.BaseUri is not null) || (ctx.Depth > 2))\n            {\n                throw new XamlObjectWriterException(SR.CannotSetBaseUri);\n            }\n        }\n\n        /// <summary>\n        /// Process the call to a MarkupExtension.ProvideValue() and assign the result\n        /// </summary>\n        private void Logic_AssignProvidedValue(ObjectWriterContext ctx)\n        {\n            bool handled = Logic_ProvideValue(ctx);\n            if (!handled && ctx.ParentProperty is not null)\n            {\n                Logic_DoAssignmentToParentProperty(ctx);\n            }\n        }\n\n        // Returns true if the assignment was handled by a SetMarkupExtensionHandler\n        private bool Logic_ProvideValue(ObjectWriterContext ctx)\n        {","sourceCodeStart":1651,"sourceCodeEnd":1687,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs#L1651-L1687","documentation":"XamlObjectWriterException thrown by Logic_CheckBaseUri when a BaseUri property assignment is attempted either after BaseUri was already set or on a non-root element (context depth greater than 2). BaseUri may only be assigned once, on the root element of the XAML stream.","triggerScenarios":"Writing a second xml:base / BaseUri value into the node stream, or assigning BaseUri while the object writer sits deeper than the root element (Depth > 2, i.e. past the root's StartObject/StartMember slots).","commonSituations":"Merging or concatenating XAML fragments that each declare xml:base; templates/includes that re-declare base URI inside nested elements; custom node-stream transformers duplicating the BaseUri member on child objects.","solutions":["Strip duplicate BaseUri/xml:base declarations from non-root elements before writing the node stream.","Set BaseUri exactly once, only for the root object (Depth <= 2 in the writer).","If merging documents, keep only the outermost document's base URI and drop inner ones.","Guard the write site: check ctx.BaseUri is null and ctx.Depth <= 2 before emitting the BaseUri member."],"exampleFix":"// before\nWriteBaseUri(childObject, \"file:///merged.xaml\"); // non-root: throws\n// after\nif (ctx.BaseUri == null && ctx.Depth <= 2)\n    WriteBaseUri(rootObject, \"file:///merged.xaml\");\n// else skip — nested fragments inherit the root base URI","handlingStrategy":"validation","validationCode":"if (ctx.BaseUri != null || ctx.Depth > 2)\n    return; // skip: BaseUri already set or not on root element","typeGuard":"static bool CanSetBaseUri(ObjectWriterContext ctx) => ctx.BaseUri == null && ctx.Depth <= 2;","tryCatchPattern":"try { WriteBaseUri(obj, uri); }\ncatch (XamlObjectWriterException ex) when (ex.Message == System.Xaml.SR.CannotSetBaseUri) { LogDuplicateBaseUri(uri); }","preventionTips":["Emit xml:base only on the root element of the node stream.","Strip BaseUri declarations from merged/inner XAML fragments.","Ensure exactly one BaseUri assignment per document.","Validate merged node streams for duplicate xml:base before writing."],"tags":["xaml","baseuri","root-element","duplicate-assignment"],"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"}