dotnet/wpf · error · InvalidOperationException

SR.Format(SR.DeferringLoaderNoContext…

Error message

SR.Format(SR.DeferringLoaderNoContext, nameof(DeferrableContentConverter), typeof(T).Name)

What it means

DeferrableContentConverter's private RequireService<T> helper resolves a required service (IXamlSchemaContextProvider, IXamlObjectWriterFactory, IProvideValueTarget, IRootObjectProvider) from the ambient IServiceProvider. If GetService returns null for the requested type, it throws InvalidOperationException stating the deferring loader has no context, naming the missing service type.

Solutions

  1. Invoke the converter only within a real XAML parse (Baml2006Reader/XamlXmlReader + XamlObjectWriter), which supplies all required services
  2. In test harnesses, register all four services: IXamlSchemaContextProvider, IXamlObjectWriterFactory, IProvideValueTarget, IRootObjectProvider
  3. Read the exception message to see which typeof(T).Name is missing and add that service registration

Example fix

// before
var provider = new ServiceProvider(); // no services registered
converter.ConvertFrom(provider, CultureInfo.InvariantCulture, bamlStream);
// after
var sp = new ServiceProvider();
sp.AddService(typeof(IXamlSchemaContextProvider), schemaContextProvider);
sp.AddService(typeof(IXamlObjectWriterFactory), writerFactory);
sp.AddService(typeof(IProvideValueTarget), pvt);
sp.AddService(typeof(IRootObjectProvider), rootProvider);
Defensive patterns

Strategy: validation

Validate before calling

static void RequireDeferrableServices(IServiceProvider p)
{
    foreach (var t in new[] { typeof(IXamlSchemaContextProvider), typeof(IXamlObjectWriterFactory), typeof(IProvideValueTarget), typeof(IRootObjectProvider) })
        if (p.GetService(t) == null) throw new InvalidOperationException($"Missing service: {t.Name}");
}

Type guard

static bool HasRequiredXamlServices(IServiceProvider p) =>
    p.GetService(typeof(IXamlSchemaContextProvider)) != null &&
    p.GetService(typeof(IXamlObjectWriterFactory)) != null &&
    p.GetService(typeof(IProvideValueTarget)) != null &&
    p.GetService(typeof(IRootObjectProvider)) != null;

Try / catch

try { return (DeferrableContent)converter.ConvertFrom(provider, culture, value); }
catch (InvalidOperationException ex) when (ex.Message.Contains("DeferrableContentConverter")) { /* log which service is missing from the message */ }

Prevention

When it happens

Trigger: Calling ConvertFrom with a service provider (ITypeDescriptorContext) that does not supply one of the required services — typically a hand-built ServiceProvider or a plain TypeDescriptor context with no XAML ambient services.

Common situations: Unit-testing the converter with a stub service provider that registers only some services, invoking the converter outside a XAML reader/writer session, or a custom XAML pipeline that omits IXamlObjectWriterFactory or IRootObjectProvider.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


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

Appendix: source

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

                {
                    throw new InvalidOperationException(SR.ExpectedBinaryContent);
                }

                // we shouldn't pass around the service provider
                DeferrableContent deferrableContext = new DeferrableContent(stream, schemaContext,
                    objectWriterFactory, context, rootObjectProvider.RootObject);
                return deferrableContext;
            }

            return base.ConvertFrom(context, culture, value);
        }

        private static T RequireService<T>(IServiceProvider provider) where T : class
        {
            T result = provider.GetService(typeof(T)) as T;
            if (result == null)
            {
                throw new InvalidOperationException(SR.Format(SR.DeferringLoaderNoContext, nameof(DeferrableContentConverter), typeof(T).Name));
            }
            return result;
        }
    }
}

View on GitHub (pinned to 81131a70a4)