{"record":{"id":"408d0eb21d4f02a1","repo":"dotnet/wpf","slug":"sr-savedcontextschemacontextmismatch","errorCode":null,"errorMessage":"SR.SavedContextSchemaContextMismatch","messagePattern":"SR\\.SavedContextSchemaContextMismatch","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs","lineNumber":76,"sourceCode":"        {\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            {\n                _afterBeginInitHandler = settings.AfterBeginInitHandler;\n                _beforePropertiesHandler = settings.BeforePropertiesHandler;\n                _afterPropertiesHandler = settings.AfterPropertiesHandler;\n                _afterEndInitHandler = settings.AfterEndInitHandler;\n\n                _xamlSetValueHandler = settings.XamlSetValueHandler;\n\n                _rootObjectInstance = settings.RootObjectInstance;\n                _skipDuplicatePropertyCheck = settings.SkipDuplicatePropertyCheck;\n                _skipProvideValueOnRoot = settings.SkipProvideValueOnRoot;\n                _preferUnconvertedDictionaryKeys = settings.PreferUnconvertedDictionaryKeys;\n            }\n\n            XAML3.INameScope rootNameScope = settings?.ExternalNameScope;","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs#L58-L94","documentation":"ArgumentException thrown by XamlObjectWriter.Initialize (reached from SerializeObject) when a non-null XamlSavedContext is supplied but its SchemaContext differs from the schemaContext parameter. The writer requires one consistent schema context across the reader/writer pair, otherwise type resolution would be inconsistent.","triggerScenarios":"Calling the internal Initialize/SerializeObject path with a XamlSavedContext captured from a reader built on a different XamlSchemaContext instance than the one passed to Initialize (reference inequality, not just inequality of settings).","commonSituations":"Mixing readers and writers constructed with separately created XamlSchemaContext instances; copying a saved context between two XAML load operations that each built their own schema context; refactoring that split reader/writer construction across components.","solutions":["Pass the same XamlSchemaContext instance used to create the savedContext: schemaContext == savedContext.SchemaContext must hold by reference.","Reuse a single shared XamlSchemaContext across the whole load/save pipeline instead of constructing new ones per component.","If contexts must differ, rebuild the saved context from a reader created with the writer's schema context.","Pass null savedContext if you do not need the saved context, letting the writer use the given schemaContext alone."],"exampleFix":"// before\nvar shared = new XamlSchemaContext();\nvar other = new XamlSchemaContext();\nvar writer = new XamlObjectWriter(shared, settings);\nwriter.Initialize(savedContextFrom(other), shared, settings); // mismatch\n// after\nvar shared = new XamlSchemaContext();\nvar writer = new XamlObjectWriter(shared, settings);\nwriter.Initialize(savedContextFrom(shared), shared, settings); // same instance","handlingStrategy":"validation","validationCode":"if (savedContext != null && !ReferenceEquals(schemaContext, savedContext.SchemaContext))\n    throw new InvalidOperationException(\"schemaContext must be the same instance as savedContext.SchemaContext.\");","typeGuard":"static bool ContextsMatch(XamlSchemaContext sc, XamlSavedContext saved) => saved == null || ReferenceEquals(sc, saved.SchemaContext);","tryCatchPattern":"try { writer.Initialize(schemaContext, savedContext, settings); }\ncatch (ArgumentException ex) when (ex.ParamName == \"schemaContext\") { RebuildPipelineWithSharedContext(); }","preventionTips":["Share one XamlSchemaContext instance across the whole reader/writer pipeline.","Never create a fresh XamlSchemaContext per component in a load/save chain.","Compare by reference when validating saved contexts.","Pass null savedContext when no saved state is needed."],"tags":["xaml","argumentexception","schema-context","mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}