dotnet/wpf · error · ArgumentException
SR.SavedContextSchemaContextNull
Error message
SR.SavedContextSchemaContextNull
What it means
ArgumentException thrown by the internal XamlObjectWriter(XamlSavedContext, XamlObjectWriterSettings) constructor when the supplied XamlSavedContext has a null SchemaContext. An XamlObjectWriter cannot function without a XamlSchemaContext, so the library rejects the saved context up front.
Solutions
- Ensure the XamlSavedContext was created with a non-null XamlSchemaContext before passing it to the XamlObjectWriter constructor.
- Re-create the saved context from a reader initialized with a valid XamlSchemaContext (e.g. new XamlSchemaContext()).
- If the context comes from another component, assert/validate savedContext.SchemaContext != null before constructing the writer.
Example fix
// before
var writer = new XamlObjectWriter(savedContext, settings); // savedContext.SchemaContext == null
// after
if (savedContext.SchemaContext == null)
throw new InvalidOperationException("Saved context has no schema context; recreate it from a reader with a XamlSchemaContext.");
var writer = new XamlObjectWriter(savedContext, settings); Defensive patterns
Strategy: validation
Validate before calling
if (savedContext == null || savedContext.SchemaContext == null)
throw new InvalidOperationException("savedContext requires a non-null SchemaContext."); Type guard
static bool HasSchemaContext(XamlSavedContext c) => c?.SchemaContext != null;
Try / catch
try { new XamlObjectWriter(savedContext, settings); }
catch (ArgumentException ex) when (ex.ParamName == "savedContext") { RecreateContextAndRetry(); } Prevention
- Always construct XamlSavedContext from a reader initialized with a real XamlSchemaContext.
- Validate savedContext.SchemaContext before constructing the writer.
- Avoid persisting/transporting saved contexts without their schema context.
- Keep reader and writer construction in one pipeline component so context is never dropped.
When it happens
Trigger: Passing a XamlSavedContext whose SchemaContext property is null to the internal XamlObjectWriter constructor — typically from a custom XamlReader pipeline that built the saved context without a schema context.
Common situations: Reusing a saved context captured from a partially-constructed or default-initialized reader; deserializing/persisting XAML state across AppDomains where the schema context was dropped; custom reader implementations that forget to pass the schema context through.
Related errors
- SR.NamespaceDeclarationNamespaceCannotBeNull
- SR.NamespaceDeclarationPrefixCannotBeNull
- SR.SavedContextSchemaContextMismatch
- ArgumentException(SR.Format(SR.CollectionCannotContainNulls…
- ArgumentException(SR.StringIsNullOrEmpty, nameof(fileName))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/899a574602fe92df.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs:62
public XamlObjectWriter(XamlSchemaContext schemaContext)
{
ArgumentNullException.ThrowIfNull(schemaContext);
Initialize(schemaContext, (XamlSavedContext)null, (XamlObjectWriterSettings)null);
}
public XamlObjectWriter(XamlSchemaContext schemaContext, XamlObjectWriterSettings settings)
{
ArgumentNullException.ThrowIfNull(schemaContext);
Initialize(schemaContext, (XamlSavedContext)null, settings);
}
internal XamlObjectWriter(XamlSavedContext savedContext, XamlObjectWriterSettings settings)
{
ArgumentNullException.ThrowIfNull(savedContext);
if (savedContext.SchemaContext is null)
{
throw new ArgumentException(SR.SavedContextSchemaContextNull, nameof(savedContext));
}
Initialize(savedContext.SchemaContext, savedContext, settings);
}
private void Initialize(XamlSchemaContext schemaContext, XamlSavedContext savedContext, XamlObjectWriterSettings settings)
{
_inDispose = false;
// ObjectWriter must be passed in a non-null SchemaContext. We check that here, since the CreateContext method
// will create one if a null SchemaContext was passed in.
ArgumentNullException.ThrowIfNull(schemaContext);
if (savedContext is not null && schemaContext != savedContext.SchemaContext)
{
throw new ArgumentException(SR.SavedContextSchemaContextMismatch, nameof(schemaContext));
}
if (settings is not null)
{View on GitHub (pinned to 81131a70a4)