dotnet/wpf · error · ArgumentNullException

SR.SchemaContextNotInitialized

Error message

SR.SchemaContextNotInitialized

What it means

XamlObjectReader requires a non-null XamlSchemaContext to resolve types and members while reading the object graph. The constructor guard here is on schemaContext itself; the message is thrown when the reader's schema context is missing or not initialized, since no XAML type mapping can be performed without it.

Solutions

  1. Pass a valid XamlSchemaContext, e.g. new XamlSchemaContext() or one obtained from XamlReader.SchemaContext of an existing reader.
  2. Add a null check before constructing the reader so the failure surfaces at the call site with a clear message.
  3. Use the overload XamlObjectReader(object) (or with settings) that constructs a default schema context for you when no custom context is needed.

Example fix

// before
var reader = new XamlObjectReader(instance, null); // throws
// after
var schemaContext = new XamlSchemaContext();
var reader = new XamlObjectReader(instance, schemaContext);
Defensive patterns

Strategy: type-guard

Validate before calling

if (schemaContext is null) throw new ArgumentNullException(nameof(schemaContext));
var reader = new XamlObjectReader(instance, schemaContext);

Type guard

bool HasValidSchemaContext(XamlSchemaContext ctx) => ctx is not null && ctx.ReferenceAssemblies is not null;

Try / catch

try { var reader = new XamlObjectReader(instance, ctx); }
catch (ArgumentNullException) { ctx = new XamlSchemaContext(); /* retry with default context */ }

Prevention

When it happens

Trigger: Passing null as the XamlSchemaContext argument to XamlObjectReader(object, XamlSchemaContext) or XamlObjectReader(object, XamlSchemaContext, XamlObjectReaderSettings) — directly rejected via ArgumentNullException (XamlObjectReader.cs:46 area). Also occurs on a XamlSchemaContext that was never initialized (not built from reference assemblies) before use.

Common situations: Constructing XamlObjectReader programmatically and passing null because the context came from an uninitialized/failed factory call; copying a pattern from overloads that derive the context automatically; DI container injecting a null schema context.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs:46

        public XamlObjectReader(object instance)
            : this(instance, (XamlObjectReaderSettings)null)
        {
        }

        public XamlObjectReader(object instance, XamlObjectReaderSettings settings)
            : this(instance, new XamlSchemaContext(), settings)
        {
        }

        public XamlObjectReader(object instance, XamlSchemaContext schemaContext)
            : this(instance, schemaContext, null)
        {
        }

        public XamlObjectReader(object instance, XamlSchemaContext schemaContext, XamlObjectReaderSettings settings)
        {
            this.schemaContext = schemaContext ?? throw new ArgumentNullException(nameof(schemaContext));
            this.settings = settings ?? new XamlObjectReaderSettings();
            nodes = new Stack<MarkupInfo>();
            currentXamlNode = new XamlNode(XamlNode.InternalNodeType.StartOfStream);

            var context = new SerializerContext(schemaContext, this.settings) { RootType = instance?.GetType() };

            var rootObject = ObjectMarkupInfo.ForObject(instance, context, null, true);

            // While we still have name scopes to do, do them. We postpone name scopes so that we can appropriately
            // resolve references from inside a namescope to objects outside of the name scope (which may also occur
            // later in the XAML).
            //
            while (context.PendingNameScopes.Count > 0)
            {
                var recordInfo = context.PendingNameScopes.Dequeue();
                recordInfo.Resume(context);
            }

View on GitHub (pinned to 81131a70a4)