dotnet/wpf · error · InvalidOperationException

SR.ExpectedBamlSchemaContext

Error message

SR.ExpectedBamlSchemaContext

What it means

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.

Solutions

  1. Ensure the content is loaded through the WPF BAML reader path (XamlReader.Load on a BAML stream / Application.LoadComponent), which installs a Baml2006SchemaContext
  2. Remove any custom IXamlSchemaContextProvider registration that supplies a non-BAML XamlSchemaContext
  3. If parsing XAML text rather than BAML, do not use DeferrableContentConverter — deferred content is a BAML-only feature

Example fix

// before
var reader = new Baml2006Reader(stream, new XamlSchemaContext());
// after
var reader = new Baml2006Reader(stream, new Baml2006SchemaContext());
Defensive patterns

Strategy: validation

Validate before calling

var scp = provider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;
if (scp?.SchemaContext is not Baml2006SchemaContext)
    throw new InvalidOperationException("Deferrable content requires Baml2006SchemaContext; use the BAML reader path.");

Type guard

static bool IsBamlSchemaContext(IServiceProvider p) =>
    (p.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider)?.SchemaContext is Baml2006SchemaContext;

Try / catch

try { return (DeferrableContent)converter.ConvertFrom(ctx, culture, stream); }
catch (InvalidOperationException ex) when (ex.Message.Contains("schema context")) { /* fall back to non-deferred load */ }

Prevention

When it happens

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

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

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DeferrableContentConverter.cs:36

            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value != null)
            {
                ArgumentNullException.ThrowIfNull(context);

                XamlSchemaContext xamlSchemaContext =
                    RequireService<IXamlSchemaContextProvider>(context).SchemaContext;
                Baml2006SchemaContext schemaContext = xamlSchemaContext as Baml2006SchemaContext;
                if (schemaContext == null)
                {
                    throw new InvalidOperationException(SR.ExpectedBamlSchemaContext);
                }

                IXamlObjectWriterFactory objectWriterFactory =
                    RequireService<IXamlObjectWriterFactory>(context);
                IProvideValueTarget ipvt =
                    RequireService<IProvideValueTarget>(context);
                IRootObjectProvider rootObjectProvider =
                    RequireService<IRootObjectProvider>(context);

                ResourceDictionary dictionary = ipvt.TargetObject as ResourceDictionary;
                if (dictionary == null)
                {
                    throw new InvalidOperationException(SR.ExpectedResourceDictionaryTarget);
                }

                Stream stream = value as Stream;
                if (stream == null)
                {

View on GitHub (pinned to 81131a70a4)