dotnet/wpf · error · InvalidOperationException
SR.NameScopeOnRootInstance
Error message
SR.NameScopeOnRootInstance
What it means
The ObjectWriterContext constructor sets up the name scope for the root object frame. If the root frame has no instance yet and its XamlType IsNameScope (i.e. the root itself represents a name scope such as x:NameScope semantics), the writer cannot establish a name scope dictionary and throws InvalidOperationException with SR.NameScopeOnRootInstance.
Solutions
- Ensure the root object is instantiated before the writer's name-scope setup — supply a valid root instance (XamlObjectWriterSettings.RootObjectInstance) or fix the preceding creation step.
- Do not make the root element a name-scope type; wrap it in a concrete root object (e.g. Page, UserControl).
- Check earlier writer errors: a failed root instantiation leaving Instance null is often the underlying cause — fix that error first.
Example fix
// before: root type is a name scope and instance is null
var writer = new XamlObjectWriter(schemaContext);
// after: provide a concrete root instance
var settings = new XamlObjectWriterSettings { RootObjectInstance = new MyRoot() };
var writer = new XamlObjectWriter(schemaContext, settings); Defensive patterns
Strategy: validation
Validate before calling
if (rootInstance is null && schemaContext.GetXamlType(rootType).IsNameScope)
throw new InvalidOperationException("Root of a XAML tree cannot be a name-scope type with a null instance"); Type guard
static bool HasValidRoot(object? root, XamlType type) => root is not null || !type.IsNameScope;
Try / catch
try { RunObjectWriter(xamlReader); }
catch (InvalidOperationException ex) when (ex.Message.Contains("name scope"))
{ /* supply RootObjectInstance or change the root element type */ } Prevention
- Always give XamlObjectWriter a concrete, instantiable root (via RootObjectInstance or the stream's root element).
- Do not use name-scope pseudo-types as the document root of XAML you write at runtime.
- Handle earlier instantiation exceptions so a null root instance never reaches the writer.
When it happens
Trigger: Running XamlObjectWriter against a XAML tree whose root element is a name-scope type but has a null instance at depth 1 — e.g. writing XAML where the root is declared as a name scope without an instantiable root object.
Common situations: Custom XAML writing pipelines that provide no root instance while the root type is flagged IsNameScope; misuse of x:XData/x:NameScope-style roots; generated XAML whose root object failed to instantiate earlier in the pipeline.
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
- IAmbientProvider
- IXamlSchemaContextProvider
- SR.Format(SR.CannotHavePropertyInTemplate…
- SR.Format(SR.NameNotFound, propertyValue.ChildName)
- SR.Format(SR.NameScopeDuplicateNamesNotAllowed, childName)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/da1844b95eb814df.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs:883
if (instance == frame.Instance)
{
return true;
}
frame = (ObjectWriterFrame)frame.Previous;
}
return false;
}
private XAML3.INameScopeDictionary HuntAroundForARootNameScope(ObjectWriterFrame rootFrame)
{
Debug.Assert(rootFrame.Depth == 1);
object inst = rootFrame.Instance;
if (inst is null && rootFrame.XamlType.IsNameScope)
{
throw new InvalidOperationException(SR.NameScopeOnRootInstance);
}
XAML3.INameScopeDictionary nameScopeDictionary = null;
nameScopeDictionary = inst as XAML3.INameScopeDictionary;
if (nameScopeDictionary is null)
{
if (inst is XAML3.INameScope nameScope)
{
nameScopeDictionary = new NameScopeDictionary(nameScope);
}
}
// If the root instance isn't a name scope
// then perhaps it designated a property as the name scope.
if (nameScopeDictionary is null)
{View on GitHub (pinned to 81131a70a4)