dotnet/wpf · error · NotSupportedException

SR.Format(SR.DeferringLoaderNoSave…

Error message

SR.Format(SR.DeferringLoaderNoSave, nameof(TemplateContentLoader))

What it means

TemplateContentLoader only supports deferred loading; its Save override always throws NotSupportedException (SR.DeferringLoaderNoSave). Deferring content means the loader produces a XamlReader for later consumption and cannot serialize objects back to XAML.

Solutions

  1. Do not use TemplateContentLoader on the save path; let the standard XAML writer serialize the template instead.
  2. If a template must be saved, load its deferred content fully (drain the XamlReader into an object graph) before serialization.
  3. Wrap save operations in try/catch on NotSupportedException and fall back to full materialization of the template.

Example fix

// before
var reader = loader.Save(template, serviceProvider); // always throws
// after
// materialize first, then let XamlXmlWriter handle serialization
var loaded = loader.Load(deferredReader, serviceProvider);
xamlWriter.WriteNode(xamlReader = new XamlObjectReader(loaded));
Defensive patterns

Strategy: try-catch

Validate before calling

if (loader is TemplateContentLoader) throw new NotSupportedException("TemplateContentLoader does not support Save; use default serialization");

Try / catch

try { writerOutput = loader.Save(value, provider); }
catch (NotSupportedException ex) { log(ex.Message); writerOutput = SerializeViaDefaultWriter(value); }

Prevention

When it happens

Trigger: Calling TemplateContentLoader.Save(object, IServiceProvider) — e.g. XamlServices / XamlObjectWriter invoking the loader's Save path during serialization of a template.

Common situations: Trying to serialize (save) a template with deferred content back to XAML, or configuring DeferringLoader attributes and then running a save pass.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContentLoader.cs:31

            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)