{"record":{"id":"75d02d5a3f7605c3","repo":"dotnet/wpf","slug":"ixamlschemacontextprovider","errorCode":null,"errorMessage":"IXamlSchemaContextProvider","messagePattern":"IXamlSchemaContextProvider","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StaticResourceExtension.cs","lineNumber":198,"sourceCode":"                // If we didn't find a new value in this part of deferred content\n                // then use the existing prefetchedValue (DeferredResourceReference)\n                if (value == DependencyProperty.UnsetValue)\n                {\n                    value = allowDeferredReference\n                             ? prefetchedValue\n                             : prefetchedValue.GetValue(BaseValueSourceInternal.Unknown);\n                }\n\n            }\n            return value;\n        }\n\n        private ResourceDictionary FindTheResourceDictionary(IServiceProvider serviceProvider, bool isDeferredContentSearch)\n        {\n            var schemaContextProvider = serviceProvider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;\n            if (schemaContextProvider == null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, \"IXamlSchemaContextProvider\"));\n            }\n\n            var ambientProvider = serviceProvider.GetService(typeof(IAmbientProvider)) as IAmbientProvider;\n            if (ambientProvider == null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, \"IAmbientProvider\"));\n            }\n\n            XamlSchemaContext schemaContext = schemaContextProvider.SchemaContext;\n\n            // This seems like a lot of work to do on every Provide Value\n            // but that types and properties are cached in the schema.\n            //\n            XamlType feXType = schemaContext.GetXamlType(typeof(FrameworkElement));\n            XamlType styleXType = schemaContext.GetXamlType(typeof(Style));\n            XamlType templateXType = schemaContext.GetXamlType(typeof(FrameworkTemplate));\n            XamlType appXType = schemaContext.GetXamlType(typeof(Application));\n            XamlType fceXType = schemaContext.GetXamlType(typeof(FrameworkContentElement));","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StaticResourceExtension.cs#L180-L216","documentation":"StaticResourceExtension.ProvideValue (via FindTheResourceDictionary) only works inside a XAML loader context. It requests IXamlSchemaContextProvider from the IServiceProvider and throws InvalidOperationException (MarkupExtensionNoContext naming \"IXamlSchemaContextProvider\") when the service is unavailable.","triggerScenarios":"Calling new StaticResourceExtension(key).ProvideValue(serviceProvider) manually with a bare ServiceProvider that lacks IXamlSchemaContextProvider, or evaluating the extension outside XAML parsing (e.g. in code, a designer shim, or a unit test).","commonSituations":"Unit-testing markup extensions with a stub IServiceProvider; reusing StaticResource markup extension instances programmatically; running the extension in a context where XAML schema context services were never registered.","solutions":["Let the XAML parser invoke the extension (use it in XAML) instead of calling ProvideValue manually.","If calling in code, use FindResource/TryFindResource on the FrameworkElement instead of the markup extension.","Supply a full IServiceProvider configured by a XamlXmlParser/XamlObjectWriter that provides IXamlSchemaContextProvider."],"exampleFix":"// before\nvar v = new StaticResourceExtension(\"MyKey\").ProvideValue(new ServiceProvider());\n// after\nvar v = targetElement.TryFindResource(\"MyKey\");","handlingStrategy":"fallback","validationCode":"var schemaCtxProvider = serviceProvider?.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;\nif (schemaCtxProvider == null) { /* use FindResource instead of the markup extension */ }","typeGuard":"static bool HasXamlContext(IServiceProvider sp) => sp?.GetService(typeof(IXamlSchemaContextProvider)) != null;","tryCatchPattern":"try { value = ext.ProvideValue(sp); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"IXamlSchemaContextProvider\")) { value = element.TryFindResource(key); }","preventionTips":["Never call markup extension ProvideValue manually; use extensions only in XAML.","Prefer FindResource/TryFindResource for programmatic resource lookup.","In tests, use the real XAML pipeline (XamlXmlLoader) rather than stub service providers."],"tags":["markup-extension","xaml","invalid-state","wpf"],"backgroundTag":"invalid-state-transition","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"}