{"record":{"id":"899a574602fe92df","repo":"dotnet/wpf","slug":"sr-savedcontextschemacontextnull","errorCode":null,"errorMessage":"SR.SavedContextSchemaContextNull","messagePattern":"SR\\.SavedContextSchemaContextNull","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs","lineNumber":62,"sourceCode":"\n        public XamlObjectWriter(XamlSchemaContext schemaContext)\n        {\n            ArgumentNullException.ThrowIfNull(schemaContext);\n            Initialize(schemaContext, (XamlSavedContext)null, (XamlObjectWriterSettings)null);\n        }\n\n        public XamlObjectWriter(XamlSchemaContext schemaContext, XamlObjectWriterSettings settings)\n        {\n            ArgumentNullException.ThrowIfNull(schemaContext);\n            Initialize(schemaContext, (XamlSavedContext)null, settings);\n        }\n\n        internal XamlObjectWriter(XamlSavedContext savedContext, XamlObjectWriterSettings settings)\n        {\n            ArgumentNullException.ThrowIfNull(savedContext);\n            if (savedContext.SchemaContext is null)\n            {\n                throw new ArgumentException(SR.SavedContextSchemaContextNull, nameof(savedContext));\n            }\n\n            Initialize(savedContext.SchemaContext, savedContext, settings);\n        }\n\n        private void Initialize(XamlSchemaContext schemaContext, XamlSavedContext savedContext, XamlObjectWriterSettings settings)\n        {\n            _inDispose = false;\n            // ObjectWriter must be passed in a non-null SchemaContext.  We check that here, since the CreateContext method\n            // will create one if a null SchemaContext was passed in.\n            ArgumentNullException.ThrowIfNull(schemaContext);\n            if (savedContext is not null && schemaContext != savedContext.SchemaContext)\n            {\n                throw new ArgumentException(SR.SavedContextSchemaContextMismatch, nameof(schemaContext));\n            }\n\n            if (settings is not null)\n            {","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar writer = new XamlObjectWriter(savedContext, settings); // savedContext.SchemaContext == null\n// after\nif (savedContext.SchemaContext == null)\n    throw new InvalidOperationException(\"Saved context has no schema context; recreate it from a reader with a XamlSchemaContext.\");\nvar writer = new XamlObjectWriter(savedContext, settings);","handlingStrategy":"validation","validationCode":"if (savedContext == null || savedContext.SchemaContext == null)\n    throw new InvalidOperationException(\"savedContext requires a non-null SchemaContext.\");","typeGuard":"static bool HasSchemaContext(XamlSavedContext c) => c?.SchemaContext != null;","tryCatchPattern":"try { new XamlObjectWriter(savedContext, settings); }\ncatch (ArgumentException ex) when (ex.ParamName == \"savedContext\") { RecreateContextAndRetry(); }","preventionTips":["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."],"tags":["xaml","argumentexception","null-check","schema-context"],"backgroundTag":"null-argument","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"}