dotnet/wpf · error · InvalidOperationException

IXamlSchemaContextProvider

Error message

IXamlSchemaContextProvider

What it means

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.

Solutions

  1. Let the XAML parser invoke the extension (use it in XAML) instead of calling ProvideValue manually.
  2. If calling in code, use FindResource/TryFindResource on the FrameworkElement instead of the markup extension.
  3. Supply a full IServiceProvider configured by a XamlXmlParser/XamlObjectWriter that provides IXamlSchemaContextProvider.

Example fix

// before
var v = new StaticResourceExtension("MyKey").ProvideValue(new ServiceProvider());
// after
var v = targetElement.TryFindResource("MyKey");
Defensive patterns

Strategy: fallback

Validate before calling

var schemaCtxProvider = serviceProvider?.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;
if (schemaCtxProvider == null) { /* use FindResource instead of the markup extension */ }

Type guard

static bool HasXamlContext(IServiceProvider sp) => sp?.GetService(typeof(IXamlSchemaContextProvider)) != null;

Try / catch

try { value = ext.ProvideValue(sp); }
catch (InvalidOperationException ex) when (ex.Message.Contains("IXamlSchemaContextProvider")) { value = element.TryFindResource(key); }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/75d02d5a3f7605c3. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StaticResourceExtension.cs:198

                // If we didn't find a new value in this part of deferred content
                // then use the existing prefetchedValue (DeferredResourceReference)
                if (value == DependencyProperty.UnsetValue)
                {
                    value = allowDeferredReference
                             ? prefetchedValue
                             : prefetchedValue.GetValue(BaseValueSourceInternal.Unknown);
                }

            }
            return value;
        }

        private ResourceDictionary FindTheResourceDictionary(IServiceProvider serviceProvider, bool isDeferredContentSearch)
        {
            var schemaContextProvider = serviceProvider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;
            if (schemaContextProvider == null)
            {
                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, "IXamlSchemaContextProvider"));
            }

            var ambientProvider = serviceProvider.GetService(typeof(IAmbientProvider)) as IAmbientProvider;
            if (ambientProvider == null)
            {
                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, "IAmbientProvider"));
            }

            XamlSchemaContext schemaContext = schemaContextProvider.SchemaContext;

            // This seems like a lot of work to do on every Provide Value
            // but that types and properties are cached in the schema.
            //
            XamlType feXType = schemaContext.GetXamlType(typeof(FrameworkElement));
            XamlType styleXType = schemaContext.GetXamlType(typeof(Style));
            XamlType templateXType = schemaContext.GetXamlType(typeof(FrameworkTemplate));
            XamlType appXType = schemaContext.GetXamlType(typeof(Application));
            XamlType fceXType = schemaContext.GetXamlType(typeof(FrameworkContentElement));

View on GitHub (pinned to 81131a70a4)