dotnet/wpf · error · InvalidOperationException
SR.Format(SR.DeferringLoaderNoContext…
Error message
SR.Format(SR.DeferringLoaderNoContext, nameof(TemplateContentLoader), typeof(T).Name)
What it means
TemplateContentLoader.RequireService<T> fetches a required XAML service from the IServiceProvider; if the service is absent it throws InvalidOperationException (SR.DeferringLoaderNoContext), meaning TemplateContentLoader is being used outside a proper XAML parse/save context.
Solutions
- Only use TemplateContentLoader through a normal XAML load/save pipeline where System.Xaml supplies its services.
- In custom pipelines, pass an IServiceProvider backed by a real XamlSchemaContext (e.g. from XamlXmlReader/Writer).
- Wrap loader calls in try/catch on InvalidOperationException and surface which service typeof(T).Name was missing.
- Register the expected service on a custom service provider if you must fake the context in tests.
Example fix
// before loader.Load(reader, new EmptyServiceProvider()); // no services // after using var reader = new XamlXmlReader(stream); // provides schema context services var value = loader.Load(reader, reader.SchemaContext != null ? serviceProvider : null);
Defensive patterns
Strategy: validation
Validate before calling
bool hasContext = provider?.GetService(typeof(IXamlSchemaContextProvider)) != null;
if (!hasContext) throw new InvalidOperationException("TemplateContentLoader requires a System.Xaml parse context"); Type guard
static bool HasRequiredServices(IServiceProvider p) => p?.GetService(typeof(IXamlSchemaContextProvider)) != null;
Try / catch
try { value = loader.Load(reader, provider); }
catch (InvalidOperationException ex) { log(ex.Message); value = XamlServices.Load(readerStream); } Prevention
- Only invoke TemplateContentLoader from within a real XamlReader/Writer pipeline
- Don't hand-roll IServiceProviders in production code
- In tests, back the provider with a XamlSchemaContext
When it happens
Trigger: Calling Load or Save on TemplateContentLoader while passing an IServiceProvider that does not supply the required service (e.g. IXamlSchemaContextProvider / IAmbientProvider), or passing a null/empty provider.
Common situations: Invoking the deferring content loader manually in unit tests or custom XAML pipelines without a real XamlXmlReader/Writer context; reusing the loader outside System.Xaml parsing.
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
- NotImplementedException
- SR.Format(SR.DeferringLoaderNoContext…
- SR.Format(SR.MarkupExtensionNoContext, GetType().Name…
- SR.Format(SR.MarkupExtensionNoContext, GetType().Name…
- SR.MarkupExtensionNoContext
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/324228c231bb6d56.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContentLoader.cs:24
namespace System.Windows
{
public class TemplateContentLoader : XamlDeferringLoader
{
public override object Load(XamlReader xamlReader, IServiceProvider serviceProvider)
{
ArgumentNullException.ThrowIfNull(serviceProvider);
ArgumentNullException.ThrowIfNull(xamlReader);
IXamlObjectWriterFactory factory = RequireService<IXamlObjectWriterFactory>(serviceProvider);
return new TemplateContent(xamlReader, factory, serviceProvider);
}
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(TemplateContentLoader), typeof(T).Name));
}
return result;
}
public override XamlReader Save(object value, IServiceProvider serviceProvider)
{
throw new NotSupportedException(SR.Format(SR.DeferringLoaderNoSave, nameof(TemplateContentLoader)));
}
}
}
View on GitHub (pinned to 81131a70a4)