{"record":{"id":"664c99071c7a8350","repo":"dotnet/wpf","slug":"sr-expectedbamlschemacontext","errorCode":null,"errorMessage":"SR.ExpectedBamlSchemaContext","messagePattern":"SR\\.ExpectedBamlSchemaContext","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DeferrableContentConverter.cs","lineNumber":36,"sourceCode":"            {\n                return true;\n            }\n\n            return base.CanConvertFrom(context, sourceType);\n        }\n\n        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)\n        {\n            if (value != null)\n            {\n                ArgumentNullException.ThrowIfNull(context);\n\n                XamlSchemaContext xamlSchemaContext =\n                    RequireService<IXamlSchemaContextProvider>(context).SchemaContext;\n                Baml2006SchemaContext schemaContext = xamlSchemaContext as Baml2006SchemaContext;\n                if (schemaContext == null)\n                {\n                    throw new InvalidOperationException(SR.ExpectedBamlSchemaContext);\n                }\n\n                IXamlObjectWriterFactory objectWriterFactory =\n                    RequireService<IXamlObjectWriterFactory>(context);\n                IProvideValueTarget ipvt =\n                    RequireService<IProvideValueTarget>(context);\n                IRootObjectProvider rootObjectProvider =\n                    RequireService<IRootObjectProvider>(context);\n\n                ResourceDictionary dictionary = ipvt.TargetObject as ResourceDictionary;\n                if (dictionary == null)\n                {\n                    throw new InvalidOperationException(SR.ExpectedResourceDictionaryTarget);\n                }\n\n                Stream stream = value as Stream;\n                if (stream == null)\n                {","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DeferrableContentConverter.cs#L18-L54","documentation":"DeferrableContentConverter.ConvertFrom converts deferred (BAML) content streams into DeferrableContent objects. It requires the XAML schema context supplied via the service provider to be a Baml2006SchemaContext; a plain XamlSchemaContext cannot handle BAML 2006 deferred content, so the converter throws InvalidOperationException immediately.","triggerScenarios":"Calling ConvertFrom (directly or through a XAML loader path such as XamlReader / DeferringLoader) with a service context whose IXamlSchemaContextProvider.SchemaContext is not a Baml2006SchemaContext — e.g. parsing with a custom or generic XamlSchemaContext instead of the WPF BAML reader.","commonSituations":"Loading compiled BAML resources through a hand-built System.Xaml reader pipeline, using XamlXmlReader with a DeferringLoader registration meant for BAML, or hosting WPF XAML parsing inside a custom XamlObjectWriterFactory where the schema context was replaced.","solutions":["Ensure the content is loaded through the WPF BAML reader path (XamlReader.Load on a BAML stream / Application.LoadComponent), which installs a Baml2006SchemaContext","Remove any custom IXamlSchemaContextProvider registration that supplies a non-BAML XamlSchemaContext","If parsing XAML text rather than BAML, do not use DeferrableContentConverter — deferred content is a BAML-only feature"],"exampleFix":"// before\nvar reader = new Baml2006Reader(stream, new XamlSchemaContext());\n// after\nvar reader = new Baml2006Reader(stream, new Baml2006SchemaContext());","handlingStrategy":"validation","validationCode":"var scp = provider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;\nif (scp?.SchemaContext is not Baml2006SchemaContext)\n    throw new InvalidOperationException(\"Deferrable content requires Baml2006SchemaContext; use the BAML reader path.\");","typeGuard":"static bool IsBamlSchemaContext(IServiceProvider p) =>\n    (p.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider)?.SchemaContext is Baml2006SchemaContext;","tryCatchPattern":"try { return (DeferrableContent)converter.ConvertFrom(ctx, culture, stream); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"schema context\")) { /* fall back to non-deferred load */ }","preventionTips":["Always load BAML through XamlReader/Application.LoadComponent rather than custom XamlSchemaContext pipelines","Never register a generic XamlSchemaContext where WPF expects Baml2006SchemaContext","Unit-test deferred-content loading with the real WPF reader stack"],"tags":["wpf","xaml","baml"],"backgroundTag":"type-mismatch","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"}